Re: [racket-users] Storing functions in a hash

2015-05-20 Thread Matthias Felleisen

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.


[racket-users] Storing functions in a hash

2015-05-20 Thread j b
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.


Re: [racket-users] Storing functions in a hash

2015-05-20 Thread Norman Gray

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.