[plt-scheme] Macro tutorial question

From: Noel Welsh (noelwelsh at gmail.com)
Date: Mon Jul 27 09:02:35 EDT 2009

On Wed, Jul 22, 2009 at 4:00 PM, Adam C.M.
Solove<asolove at u.washington.edu> wrote:
> A number of the examples in this section depend on the macro define-get/put-id, which is defined (seemingly just as the appropriate macroexpansion for one case rather than for the general case) as :
>   (define-syntax-rule (define-get/put-id id get put!)
>     (define-syntax clock
>       (syntax-id-rules (set!)
>         [(set! clock e) (put-clock! e)]
>         [(clock a (... ...)) ((get-clock) a (... ...))]
>         [clock (get-clock)])))
> I simply adapted it to the general case and got:
> (define-syntax-rule (define-get/put-id id get put!)
>     (define-syntax id
>       (syntax-id-rules (set!)
>         [(set! id e) (put! e)]
>         [(id a (... ...)) ((get) a (... ...))]
>         [id (get)])))
> Which works, surprisingly, and confuses me. Generally define-syntax does not
> evaluate the first argument but uses it as a symbol naming the item in the
> syntax, but in this case, id is a identifier which gets evaluated so that
> the passed value of id is used as the actual syntax. Does define-syntax
> check in the environment and use the literal symbol as its name unless it
> has a defined value as an identifier? I tried to look this up in the
> documentation for define-syntax but quickly got confused.

I think you're getting confusing by the example of a macro generating a macro.


(define-syntax-rule (define-get/put-id id get put!)
    (define-syntax id
      (syntax-id-rules (set!)
        [(set! id e) (put! e)]
        [(id a (... ...)) ((get) a (... ...))]
        [id (get)])))

applied to

(define-get/put-id foo foo-get foo-put!)

expands first to

(define-syntax foo
  (syntax-id-rules (set!)

which then expands once more into whatever it is that syntax-id-rules
expands into.  Notice the first expansion substitutes the value of id
into it's body expression, which is the define-syntax.


Posted on the users mailing list.