Hi all,<br><br>Just ran into some puzzling macro behaviour that I was hoping someone could explain. I have the following code:<br><br><div class="highlight"><pre><div class="line" id="LC1"><span class="c1">; Modifies a variable by applying a function to said variable</span></div>
<div class="line" id="LC2"> </div><div class="line" id="LC3"><span class="p">(</span><span class="k">define-syntax </span><span class="nv">!</span> <span class="p">(</span><span class="k">syntax-rules </span><span class="p">()</span></div>
<div class="line" id="LC4"> <span class="err">[</span><span class="p">(</span><span class="nf">!</span> <span class="nv">x</span> <span class="p">(</span><span class="nf">fn</span> <span class="nv">arg</span> <span class="o">...</span><span class="p">))</span></div>
<div class="line" id="LC5"> <span class="p">(</span><span class="k">set! </span><span class="nv">x</span> <span class="p">(</span><span class="nf">fn</span> <span class="nv">x</span> <span class="nv">arg</span> <span class="o">...</span><span class="p">))</span><span class="err">]</span></div>
<div class="line" id="LC6"> <span class="err">[</span><span class="p">(</span><span class="nf">!</span> <span class="nv">x</span> <span class="nv">fn</span><span class="p">)</span></div><div class="line" id="LC7"> <span class="p">(</span><span class="k">set! </span><span class="nv">x</span> <span class="p">(</span><span class="nf">fn</span> <span class="nv">x</span><span class="p">))</span><span class="err">]</span><span class="p">))</span></div>
<div class="line" id="LC8"> </div><div class="line" id="LC9"><span class="c1">; for example...</span></div><div class="line" id="LC10"> </div><div class="line" id="LC11"><span class="p">(</span><span class="k">define-syntax </span><span class="nv">square!</span> <span class="p">(</span><span class="k">syntax-rules </span><span class="p">()</span></div>
<div class="line" id="LC12"> <span class="err">[</span><span class="p">(</span><span class="nf">square!</span> <span class="nv">x</span><span class="p">)</span></div><div class="line" id="LC13"> <span class="p">(</span><span class="nf">!</span> <span class="nv">x</span> <span class="p">(</span><span class="nb">* </span><span class="nv">x</span><span class="p">))</span><span class="err">]</span><span class="p">))</span></div>
<div class="line" id="LC14"> </div><div class="line" id="LC15"><span class="p">(</span><span class="k">define </span><span class="nv">a</span> <span class="mi">6</span><span class="p">)</span></div><div class="line" id="LC16">
<span class="p">(</span><span class="nf">square!</span> <span class="nv">a</span><span class="p">)</span></div></pre></div>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.<br>
<br>Any ideas what's going on here?<br><br>-- <br>James Coglan<br><a href="http://github.com/jcoglan">http://github.com/jcoglan</a><br>