[racket] Macro example in Racket Guide

From: Eli Barzilay (eli at barzilay.org)
Date: Mon Jun 4 23:46:01 EDT 2012

10 minutes ago, Harry Spier wrote:
> [...]
> 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)).

When you use (clock) the macro gets that whole form, and with your
original definition, that expands to

  ((get-clock))

leading to the error.  Without that (clock a ...) pattern, the last
one in the macro definition:

  (define-syntax clock
    (syntax-id-rules (set!)
      [(set! clock e) (put-clock! e)]
      [clock (get-clock)]))

matches (because it's a pattern variable so it matches anything), and
when you pass the (clock) syntax to this version of the macro, you get
back (get-clock) -- no extra application, which means that there's no
error.

To better see what's going on, you can use this definition instead:

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

and see what you get when you enter these

  (clock)
  (clock 1 2 3)
  clock

and now comment out the middle pattern and try it again.  Also, it
would be helpful to try the macro stepper and see what goes on, and
then try it with the previous macro definition and see how it would
save the need for this exercise.

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                    http://barzilay.org/                   Maze is Life!

Posted on the users mailing list.