[racket] Need help with macro...
The DrRacket interactions window replies with 48, not an error, when your syntax is defined in the definitions window, your function is defined in the interactions area, and the example is finally run in the interactions area.
How are you making it fail?
Also, have you attempted to use the Macro Stepper? Even if you aren't actually debugging the macro, it coerces you to organize your thoughts about how your code is organized (in this case, how the expansion relates to the word 'mult' and it's actual definition.)
rac
On Feb 12, 2012, at 11:30 AM, FS Racket wrote:
> 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.
>
>
>
>
>
>
> ____________________
> Racket Users list:
> http://lists.racket-lang.org/users