[plt-scheme] Help using syntax-rules()

From: David Van Horn (dvanhorn at ccs.neu.edu)
Date: Mon Mar 15 15:24:07 EDT 2010

On 3/15/10 3:06 PM, Patrick Li wrote:
> Hi,
> I'm trying to learn Scheme's hygienic macro system, and was wondering
> how you would write something like the following:
>
> Say I think the current let is too verbose, and want to change it to this:
>
> (mylet [a 1
>             b 2
>             c 3]
>     (display a)
>     (display b)
>     (display c))
>
> I know how to do this using Lisp-style macros, but I'm not too familiar
> with how to use the ellipsis ... effectively.

The ellipses aren't terribly effective in this case since your syntax is 
(mylet [id expr ...] body ...) where the first ellipsis needs to apply 
to the preceding *two* elements, but they don't work that way.

So if you really want this syntax and you really want to write it using 
syntax-rules, then you have to parse the bindings yourself:

(define-syntax toy-let ;)
   (syntax-rules ()
     [(toy-let (x ...) b ...)
      (toy-let-helper () () (x ...) b ...)]))

(define-syntax toy-let-helper
   (syntax-rules ()
     [(toy-let-helper xs es (x e . r) . b)
      (toy-let-helper (x . xs) (e . es) r . b)]
     [(toy-let-helper (x ...) (e ...) () . b)
      (let ((x e) ...) . b)]))

David


Posted on the users mailing list.