Yes, this is over-engineered. What do you not like about 

(define h1
  (hash
   'foo (lambda () (random 100))
   'bar (lambda () (random 500))))

(displayln h1)
(displayln ((hash-ref h1 'foo)))
(displayln ((hash-ref h1 'foo)))
(displayln ((hash-ref h1 'bar)))
(displayln ((hash-ref h1 'bar)))

Once you can articulate this thought, we can help. 


On May 20, 2015, at 8:41 AM, j b wrote:

> I'm new to Racket/Scheme/Lisp.  In addition to learning how to use the 
> language, I also want to do it the "Racket way".
> 
> I am trying to put functions into a hash so I could call them using a hash 
> key lookup (I just use 'random' as a filler in this example for what I really 
> want.  I have it working (yay).  Is this the idiomatic way to do something 
> like this?  I feel like I over-engineered it by wrapping with a lambda and 
> calling with an apply.  Any feedback is appreciated.  Thanks.
> 
> 
> ; function definition helper
> (define-syntax-rule (define-hash-function f p ...)
>  (define (f) (lambda () p ...)))
> 
> ; function caller helper
> (define-syntax-rule (call-hash-function x)
>  (apply x '()))
> 
> ; 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)))
> 
> ; 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)))
> 
> -- 
> 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.

-- 
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