[plt-scheme] binding in template
At Fri, 12 May 2006 11:07:32 +0200 (CEST), "jos.koot at tiscali.nl" wrote:
> ;Hi, From the example below it seems that in the template of a syntax-
> case rule not only pattern variables are pattern-bound, but also
> variables introduced in binding positions within the template itself.
> This is most convenient. Can it be trusted to work in general?
> Greetings, Jos koot.
>
> (define-syntax (monkey stx)
> (define (gen-expr x y) #`(list #,x #,y))
> (syntax-case stx ()
> ((monkey x) #`(let ((y 0)) #,(gen-expr #'x #'y)))))
In this example, #'y isn't pattern-bound. The code would work if you
wrote it like this:
(define-syntax (monkey stx)
(define (gen-expr x) #`(list #,x y))
(syntax-case stx ()
((monkey x) #`(let ((y 0)) #,(gen-expr #'x)))))
In both cases, the macro works because all of the #'y are introduced by
the same macro invocation, and so both `y's refer to the same binding.
Your original definition is probably better, because it keeps the two
introductions of `y' close together. In fact, there's some debate on
whether my revision should have worked; see SRFI 72 for a slightly
different macro system where your original definition works and my
revision doesn't (because the two `y's are in different quasisyntax
forms).
Matthew