[plt-scheme] Macro rule selection

From: James Coglan (jcoglan at googlemail.com)
Date: Fri Jan 30 12:53:58 EST 2009

Hi all,

Just ran into some puzzling macro behaviour that I was hoping someone could
explain. I have the following code:

; Modifies a variable by applying a function to said variable

(define-syntax ! (syntax-rules ()
  [(! x (fn arg ...))
    (set! x (fn x arg ...))]
  [(! x fn)
    (set! x (fn x))]))

; for example...

(define-syntax square! (syntax-rules ()
  [(square! x)
    (! x (* x))]))

(define a 6)
(square! a)

Turns out that when I run this, 'a' turns out as 1296, or 36 squared. My
understanding of macro rule selection was that the first matching rule is
chosen and expanded, and subsequent rules are ignored. In this case, the
more specific (! x (fn arg ...)) rule appears first so I would assume that
would be used to match (! x (* x)) in the (square!) macro. It seems the (! x
fn) rule (which would also match but has lower priority) is interfering,
since if I remove this rule all is well. I want the second rule so you can
simply pass a single argument function without surrounding it in parens.

Any ideas what's going on here?

James Coglan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20090130/96320a2a/attachment.html>

Posted on the users mailing list.