John Cowan <
johnw...@gmail.com> wrote:
> On Friday, May 10, 2013 2:20:58 AM UTC-4, Tom Kwong wrote:
> > (define-syntax macro-test
> > (syntax-rules ()
> > ((_ body ...)
> > (let ((num 2))
>
> This binds num textually within the macro, which does not in fact
> contain any references to "num", so the binding is useless.
>
> > body ...))))
> >
> > (macro-test (print 1) (print (* num num)))
>
> It does not mean that num is bound in the body with which the
> macro is invoked. That can only be done by a let (or equivalent)
> that surrounds the macro call.
In other words: there is no definition of NUM in the context
in which the macro is expanded, so NUM is undefined. The NUM
inside of the macro is local to the macro and cannot be accessed
from the outside.
It may also be educational to know that
(macro-test (print 1) (print (* num num)))
certainly will *not* expand to
(let ((num 2))
(print 1)
(print (* num num)))
but rather to something like
(let ((##12867871 2))
(print 1)
(print (* num num)))
due to hygiene. This also illustrates nicely why NUM is undefined.
The effect you are trying to achieve here (name capturing) cannot be
implemented with DEFINE-SYNTAX. You will need something more low-level,
like DEFINE-MACRO (which is, of couse, frowned upon in Scheme-land).
--
Nils M Holm < n m h @ t 3 x . o r g >
www.t3x.org