[plt-scheme] Macros in the same module

From: Paulo J. Matos (pocmatos at gmail.com)
Date: Mon May 11 08:05:47 EDT 2009

On Fri, May 8, 2009 at 12:10 AM, Eli Barzilay <eli at barzilay.org> wrote:
> Note that here `bar' is used as a function, but it's a function in the
> syntax world.  Here's a version that works like Noel mentioned (IIRC):
>
>  (define-syntax (bar a)
>    (syntax-case a ()
>      [(_ name val)
>       #'(define name val)]))
>
>  (define-syntax (foo a)
>    (syntax-case a ()
>      [(_ name val)
>       #'(bar name val)]))
>
> In here, `foo' (at compile time) doesn't really call `bar' -- it just
> expands to some syntax with `bar', and syntax expansion will proceed
> to expand the resulting syntax and will call `bar' for that, which
> eventually achieves the same effect in this case.  (BTW, this can be
> considered that `foo' is calling `bar', only it doesn't use the normal
> function call protocol.)  The second version is somewhat more common
> since you can often get away with just simlpe `syntax-rules', and the
> first version is a little better if `bar' should not exist at the
> runtime level.  The second version is also more common when you want
> to perform some computations at the syntax level that go beyond the
> common Syntax -> Syntax functions.
>

Thanks for the help. However interestingly enough that works, but this doesn't:

#lang scheme

(define-syntax (bar a)
  (syntax-case a (else)
    [(_ (else body))
     #'(else body)]
    [(_ (n body))
     #'((n) body)]))


(define-syntax (foo a)
  (syntax-case a ()
    [(_ name rules ...)
     #'(case (name)
         (bar rules) ...)]))

(define op 0)

(foo op
     (0 'zero)
     (1 'one)
     (2 'two)
     (else 'else))

It seems the call to bar only works if it's in the head of the syntax?

Can you please elaborate on this issue?

Thanks again,

-- 
Paulo Jorge Matos - pocmatos at gmail.com
Webpage: http://www.personal.soton.ac.uk/pocm


Posted on the users mailing list.