[racket] beginner question about macros

From: Carl Eastlund (cce at ccs.neu.edu)
Date: Sun Dec 4 21:06:54 EST 2011

Răzvan,

Both of these are in some sense advanced macros.  If you're just
learning how to write macros, I'd suggest slightly simpler forms.

In the first case, write (my-macro (a 1) (b 2) (c 3)) instead.
Grouping the names and numbers with parentheses means there's less
work to do when parsing the macro.  Then you can write the whole thing
with one clause.

(define-syntax my-macro
  (syntax-rules ()
    [(_ (name number) ...)
     (let ([x foo])
       (name number x)
       ...)]))

In the second case, it's not clear what you're trying to accomplish.
What are new-a, second-b, and another-c supposed to be?  The way you
wrote it, they appear to just be made up, random names that aren't
bound and will immediately lead to syntax errors.  I'm sure that's not
what you meant.  Can you clarify whether these names are supposed to
be bound inside the macro or somewhere else, and how the macro is
supposed to come up with their names?

Carl Eastlund

On Sun, Dec 4, 2011 at 10:41 AM, Răzvan Rotaru <razvan.rotaru at gmail.com> wrote:
> Hi,
>
> I currently learning scheme macros, and trying to figure out how to do
> two particulars things:
>
> 1/
> (my-macro a 1 b 2 c 3)
>
> should generate:
>
> (let ((x foo))
>    (a 1 x)
>    (b 2 x)
>    (c 3 x))
>
>
> The order is important.
>
> 2/
> I need to transform somehow the symbols:
>
> (my-macro a 1 b 2 c 3)
>
> should generate:
>
> (let ((x foo))
>    (new-a 1 x)
>    (second-b 2 x)
>    (another-c 3 x))
>
> I'd like to ask for some hints about how to achieve these two goals.
> I'm currently not able to decide whether to use syntax-rules or
> syntax-case.
>
> Thanks,
> Razvan



Posted on the users mailing list.