[racket] Need help with macro...
I'm trying to write a macro that where the pre-transformation form is:
(==> exp1 exp2 ... expn)
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:
(==> 12 (+ _ 2) (* 3 _)) would become (* 3 (+ 12 2)).
I've written a couple of different versions that exhibit the same problem
(described later). Here's my most recent attempt:
(define-syntax ==>
(lambda (stx)
(define (replace-in-first exp-list new-val)
(let* ([new-exp
(map (λ (x)
(if (eq? x '_)
new-val
x))
(syntax->datum (car exp-list))) ]
[new-stx (datum->syntax stx new-exp )] )
(cons new-stx (cdr exp-list)) ))
(syntax-case stx ()
[(_ exp) #'exp]
[(_ exp1 exp2 ...)
(with-syntax ([(threaded-exp ...)
(replace-in-first (syntax->list #'(exp2 ...))
#'exp1)])
#'(==> threaded-exp ...))] )))
The problem I'm experiencing is best demonstrated with examples. First,
assume the following definition exists in the source file:
(define (mult x y) (* x y))
;;;EXAMPLES
(==> (+ 2 2)
(+ 4 _)
(+ _ 5)) ;; works fine; result is 13
(==>
(mult 3 4)
(mult _ 2)) ;; works fine; result is 24
(==>
13
(mult _ 2)
(+ 12 _)) ;; works fine; result is 38
(==>
(mult 12 2)
(mult 2 _)
(mult _ 1)) ;; FAILS withs error: "expand: unbound identifier in module
in: mult"
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?
Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20120212/4affa279/attachment.html>