[racket] Macro example in Racket Guide

From: Jon Rafkind (rafkind at cs.utah.edu)
Date: Mon Jun 4 23:39:14 EDT 2012

I think its because without the (clock a ...) pattern the input (clock) is matched to the pattern 'clock' which matches by default. The pattern for just a lone identifier should be (in terms of syntax/parse) 'clock:identifier'. The docs for make-set!-transformer suggest to use a fender expression to check that the 'clock' pattern really matched an identifier, but since you don't have fenders in `syntax-id-rules' you either just assume the single identifier clause is the only possible choice remaining, or you can check it in the template and throw a syntax-error if its not an identifier.

Also that example is somewhat bizarre.. I think the (clock a ...) template should be (get-clock) instead of ((get-clock) a ...), right?

On 06/04/2012 09:28 PM, Harry Spier wrote:
> In section 16.1.3 of the Racket Guide there is this example and explanation:
> -----------------------------
> (define-syntax clock
>   (syntax-id-rules (set!)
>     [(set! clock e) (put-clock! e)]
>     [(clock a ...) ((get-clock) a ...)]
>     [clock (get-clock)]))
>
> (define-values (get-clock put-clock!)
>   (let ([private-clock 0])
>     (values (lambda () private-clock)
>             (lambda (v) (set! private-clock v)))))
>
>
> The (clock a ...) pattern is needed because, when an identifier macro
> is used after an open parenthesis, the macro transformer is given the
> whole form, like with a non-identifier macro.
> -------------------------------------------------------
> If I run this in DrRacket and then type in the evaluation window:
>> (clock)
> It fails with "procedure application: expected procedure, given: 0 (no
> arguments)" as it should.
>
> If I now remove the line in the macro " [(clock a ...) ((get-clock) a ...)]"
> then entering:either clock or (clock) at the evaluation prompt returns 0.
> Why is it that (clock) doesn't still fail with  "procedure
> application: expected procedure, given: 0 (no arguments)" .  I.e.
> doesn't it expand to ((get-clock)).
>
> Thanks,
> Harry
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users


Posted on the users mailing list.