The case form auto-quotes the elements at the head of each branch so the correct code would be: (define (make-f) (lambda (message . arguments) (case message ((get!) (display "get")) ((size) (display "size")) (else (display "no match")))))
Hope that helps. -Fred
On Fri, Jan 20, 2012 at 7:39 AM, Roger Wilson misterrogerwilson@gmail.com wrote:
Hi,
Thanks for all the input guys. It all makes sense.
I have a question on the cond/case option though. More one for StackOverflow really but...
(define (make-f) (lambda (message . arguments) (case message (('get!) (display "get")) (('size) (display "size")) (else (display "no match")))))
(define f (make-f)) (f 'size)
"no match"
(define (make-g) (lambda (message . arguments) (display message) (newline) (cond ((eq? message 'get!) (display "get")) ((eq? message 'size) (display "size")) (else (display "no match")))))
(define g (make-g)) (g 'size)
"size"
How do I get the make-f variant using case to do what I want? I guess this is a eqv? vs eq? thing, or am I missing a quote somewhere?
Thanks, Roger.
---------- Forwarded message ---------- From: "Adrien Piérard" pierarda@iro.umontreal.ca To: Mikael mikael.rcv@gmail.com Cc: gambit-list@iro.umontreal.ca Date: Thu, 19 Jan 2012 21:50:29 +0900 Subject: Re: [gambit-list] Idiomatic Scheme Hi again,
I couldn't resist answering more :)
In particular I'm unhappy with the way the message arguments are accessed as (car arguments) rather than by explicit name. I can see that being a potential cause of bugs if the number of arguments grows and they vary with each message.
You'll probably enjoy optional parameters, then, even though my code sample is not really portable extensible:
(lambda (message #!optional arg1) (case message ((:put-name!) (push (make-blob-name arg1) unused-name-list) (randomise-list)) ...))
By the way, why sort the list? Why not also let the constructor of your data type assign the random value? I barely use DEFINE-TYPE, but I guess there's a way to make the constructor call a function to generate the random value or something like that. Unfortunately, (random init: (random-integer 42)) doesn't work.
maybe switch the cond to a case?
Indeed, that too.
P!
Français, English, 日本語, 한국어
Gambit-list mailing list Gambit-list@iro.umontreal.ca https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list
Gambit-list mailing list Gambit-list@iro.umontreal.ca https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list