j b, hello.

Depending on what this is a cut-down version of, I think you may have slightly 
over-engineered it.

> On 2015 May 20, at 13:41, j b <phra...@gmail.com> wrote:
> 
> ; function definition helper
> (define-syntax-rule (define-hash-function f p ...)
>  (define (f) (lambda () p ...)))

This defines f to be a function returning a function.

> ; function caller helper
> (define-syntax-rule (call-hash-function x)
>  (apply x '()))

...and because of that, you have to add this indirection to call it.

> ; define functions
> (define-hash-function random-foo (random 100))
> (define-hash-function random-bar (random 500))
> 
> ; put functions into hash
> (define h1 (hash
>            'foo (random-foo)
>            'bar (random-bar)))

Here, you're initialising the hash with the functions which are returned by 
random-foo and random-bar

> ; call functions from hash
> (displayln h1)
> (displayln (call-hash-function (hash-ref h1 'foo)))
> (displayln (call-hash-function (hash-ref h1 'foo)))
> (displayln (call-hash-function (hash-ref h1 'bar)))
> (displayln (call-hash-function (hash-ref h1 'bar)))

Another way is

(define-syntax-rule (define-hash-function f p ...)
  (define (f) p ...) ; or (define f (lambda () p ...))
  )
; f is just a function

(define-hash-function random-foo (random 100))
(define-hash-function random-bar (random 500))

(define h1
  (hash
   'foo random-foo
   'bar random-bar))
; ... so the hash-values are simply the functions which random-foo and 
random-bar evaluate to

; call functions from hash
(displayln h1)
(displayln ((hash-ref h1 'foo)))
(displayln ((hash-ref h1 'foo)))
(displayln ((hash-ref h1 'bar)))
(displayln ((hash-ref h1 'bar)))

; ...which means that calling the results of those hash lookups is as simple as 
an extra pair of brackets.

The extra indirection may be useful in some circumstances, for example if the 
functions produced by your define-hash-function were parameterised at the call 
to define-hash-function.  For example:

(define (make-random-function limit)
  ;; make-random-function : number? -> (-> number?)
  ;; (ie, this is a function which evaluates to a function,
  ;; namely a function which evaluates to a random number between 0 and limit)
  (lambda ()
    (random limit)))
(define h2
  (hash
   'f100 (make-random-function 100)
   'f500 (make-random-function 500)))
(displayln h2)
(displayln ((hash-ref h2 'f100)))
(displayln ((hash-ref h2 'f100)))
(displayln ((hash-ref h2 'f500)))
(displayln ((hash-ref h2 'f500)))

....but even then, you don't need the indirection for the apply.  Remember 
functions are first-class values -- they're as much 'a thing you can pass 
around' as a string is, or a number.

Have fun,

Norman


-- 
Norman Gray  :  http://nxg.me.uk
SUPA School of Physics and Astronomy, University of Glasgow, UK

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to