[plt-scheme] Macro rule selection
When I run this program, I get:
Language: Pretty Big; memory limit: 128 megabytes.
> a
36
Can you give a more specific reproduction recipe?
sam th
On Fri, Jan 30, 2009 at 12:53 PM, James Coglan <jcoglan at googlemail.com> wrote:
> 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
> http://github.com/jcoglan
>
> _________________________________________________
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
>
--
sam th
samth at ccs.neu.edu