[plt-scheme] simple macro

From: Joe Marshall (jrm at ccs.neu.edu)
Date: Thu Mar 3 09:34:44 EST 2005

Eli Barzilay <eli at barzilay.org> writes:

>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
> On Mar  2, David J. Neu wrote:
>> 
>>  (define-syntax init
>>   (syntax-rules ()
>>     ((_ sym def) (cond ((assq (quote sym) args) => cadr)
>>                        (else def)))))


> This will work:
>
>   (define-syntax (init stx)
>     (syntax-case stx ()
>       [(_ sym def)
>        (with-syntax ([args (datum->syntax-object stx 'args stx)])
>          #'(cond [(assq (quote sym) args) => cadr]
>                  (else def)))]))

As will passing ARGS in to the macro:

 (define-syntax init
   (syntax-rules ()
     ((_ sym def args) (cond ((assq (quote sym) args) => cadr)
                             (else def)))))

 (define f
   (lambda (x . args)
     (let  ((y (init y 2 args)))
       (printf "x=~a y=~a~n" x y))))




Posted on the users mailing list.