[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Macros; lexcial scope

I would like to know how you would expect macros to work in Scheme
or generally in a lexically scoped Lisp.
(I'm not talking about a specific Scheme implementation here)

Suppose Scheme supported something like ``define-macro'' with the same
syntax as ``define'', so that you could, for instance, write

   (define-macro (incr x) `(set! ,x (+ ,x 1)))

Now my question is whether macros are lexically scoped (like functions)
or whether macro expansion is performed in a purely syntactic way.

Consider the following example:

   (define x 1)
   (define-macro (m) x)

   (let ((x 2))

Would you expect that (m) evaluates to 1 or to 2?  Yes, I know, if I had
written (define-macro (m) 'x), it would evaluate to 2.  However, in
both Common Lisp (using a different syntax, of course) and C-Scheme,
(m) returns 1 in the example above.
Thus, it looks as if the first time the macro body is evaluated, the
evaluation takes place in the lexical scope of the define-macro.

Now consider a slighly more complex example:

   (define x 1)

   (let ((x 2))
     (define-macro (m) x)
     (let ((x 3))

In both Common Lisp and C-Scheme, this evaluates to 1.  My mind boggles.
The macro is local to the outer let, why is the ``x'' inside the macro
body bound to the global variable?  Are macro always implemented like
this (in lexically scoped Lisps)?  If so, why?

[I'm sorry, if the answer to my question is obvious; I'm new to Scheme
and Lisp.]

    Oliver Laumann, Technical University of Berlin, Germany.
    ...!pyramid!tub!net   or   net@TUB.BITNET