I'm trying to write a macro that where the pre-transformation form is:<div><br></div><div>(==> exp1 exp2 ... expn)</div><div><br></div><div>and where exp2 through expression expn are sexps that contain a '_' placeholder. The idea is that starting with exp2, the _ gets replaced with the previous expression. For example:</div>
<div><br></div><div>(==> 12 (+ _ 2) (* 3 _)) would become (* 3 (+ 12 2)).</div><div><br></div><div>I've written a couple of different versions that exhibit the same problem (described later). Here's my most recent attempt:</div>
<div><br></div><div><div>(define-syntax ==></div><div> (lambda (stx)</div><div> (define (replace-in-first exp-list new-val)</div><div> (let* ([new-exp </div><div> (map (λ (x) </div><div> (if (eq? x '_)</div>
<div> new-val</div><div> x))</div><div> (syntax->datum (car exp-list))) ]</div><div> [new-stx (datum->syntax stx new-exp )] )</div><div> (cons new-stx (cdr exp-list)) ))</div>
<div> </div><div> (syntax-case stx ()</div><div> [(_ exp) #'exp]</div><div> [(_ exp1 exp2 ...)</div><div> </div><div> (with-syntax ([(threaded-exp ...)</div><div> (replace-in-first (syntax->list #'(exp2 ...)) #'exp1)])</div>
<div> #'(==> threaded-exp ...))] )))</div></div><div><br></div><div>The problem I'm experiencing is best demonstrated with examples. First, assume the following definition exists in the source file:</div>
<div><br></div><div>(define (mult x y) (* x y))</div><div><br></div><div>;;;EXAMPLES</div><div><div>(==> (+ 2 2)</div><div> (+ 4 _)</div><div> (+ _ 5)) ;; works fine; result is 13</div></div><div><br></div><div>
<div>(==> </div><div> (mult 3 4)</div><div> (mult _ 2)) ;; works fine; result is 24</div></div><div><br></div><div><div>(==></div><div> 13</div><div> (mult _ 2)</div><div> (+ 12 _)) ;; works fine; result is 38</div>
</div><div><br></div><div><div>(==></div><div> (mult 12 2)</div><div> (mult 2 _)</div><div> (mult _ 1)) ;; FAILS withs error: "expand: unbound identifier in module in: mult"</div></div><div><br></div><div>So far as I can tell, the macro fails with this error when the number of expressions is greater than 2 and when the sexps contain 2 or more references to a user-defined function. I'm guessing that somehow the macro fails to provide appropriate lexical context for the subsequent calls to the user-defined function (or something like that) but I'm stumped. Any ideas?</div>
<div><br></div><div>Thanks.</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div> </div>