syntax-rules
in Scheme are "hygienic" and "referentially transparent" and must preserve Scheme's lexical scoping. From my understanding, this means that during the macro expansion phase, the expander would need to know about lambda
and define
.
- The expander needs to know about
lambda
. Suppose we have this code:
If the expander did not know about the(define x 1) ((lambda (x) x) 2)
lambda
special form, it would consider the twox
s in(lambda (x) x)
to be bound to thex
in(define x 1)
, which is incorrect. - The expander needs to know about
define
, so that it knows where (i.e. in which scope) a particular identifier is defined. In addition, suppose we have this code:
In order to correctly determine that both(define n 1) (define f (lambda (x y) (+ x y))) (define lambda f) (lambda n n)
n
in(lambda n n)
refer to(define n 1)
, the expander has to understand that(define lambda f)
has changed the meaning oflambda
(and therefore the expander has to stop using special rules for handlinglmabda
in this scope).
What other special forms does the macro expander need to know about? Does it need to know about set!
?
The examples seem to be about lexical scoping, not macro expansion.
The binding of x in the second line shadows that in the first.
Similarly in the second example
(define lambda f)
bindslambda
in the region following the define; there is no macro expansion.The identifier
lambda
can be used as a keyword in a syntactic extension (macro); lexical scoping applies normally, there are no special rules:But: