[racket] Need help with macro...

From: Richard Cleis (rcleis at me.com)
Date: Sun Feb 12 21:48:56 EST 2012

On Feb 12, 2012, at 2:53 PM, FS Racket wrote:

> In the example that doesn't work, the first mult is needed for another use of ==>. In the simpler examples, it is not. Notice that the orphaned mult occurs on the second step when another use is expanded, while in the simpler examples another use of ==> does not occur.  In the simple cases, mult is known to the expanded code; in the example that doesn't work, mult is not known to repeated use of the macro. Another clue is: if you run the test in the interactions window, it works because the definition of mult (in definitions) is automatically provided for interactions.
> I haven't even read your macro; the amazing and amusing macro stepper knows all and tells all.
> Your comment caused me to think about why the repeated calls to ==> would 'mult' to become "unknown".  Eventually, I realized that the syntax->datum call was probably doing too much; i.e. it was stripping away too much information so when I built back up the new expression, the binding of 'mult' was lost.  
> At least, that's my guess.  I think the Racket documentation is fantastic but I find a bit of experimentation is necessary to figure some things out and that's what I've come up with.  (Perhaps an experience macrologist can comment?).  
> Anyhow, here's a version that works (er...at least it seems to work so far...):
> (define-syntax ==>
>   (lambda (stx)
>     (define (replace-in-first exp-list new-val)
>       (let* ([new-exp 
>               (map (λ (x) 
>                      (if (eq? (syntax-e x) '_)
>                          new-val
>                          x))
>                    (syntax->list (car exp-list))) ]

It's nice to make the macro processor do the work for you. In Matthias Felleisen's example, 'it' is born in the environment of the program, then the macro uses that same 'it' for the 'let' assignment and the arguments in the supplied expressions. Your macro requires more writing because it doesn't take advantage of such scope control and it doesn't use ellipses to form the loop.

I'm reading the macro chapter in the guide now (totally coincidentally), so I am curious to find out if there is a new way to write syntax-rules for the cases for which they have worked so well in the past.


> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20120212/4a012e29/attachment-0001.html>

Posted on the users mailing list.