[gambit-list] eval order, define vs define-macro

Stephane Le Cornec coleste at videotron.ca
Thu May 11 16:19:35 EDT 2006


At 11:17 -0400 2006/05/11, Marc Feeley wrote:
<snip>
>Assume the following code is in the file "test.scm":
>
>(define (foo x) x)
>(define-macro (bar x) `(+ ,x ,(foo x)))
>(define (baz x) (bar x))
>
>and you compile this code with
>
>      gsc -dynamic test.scm
>
>and then run the code with
>
>      gsi test.o1
>
>Ask yourself when the variables foo and baz will be set.
>
<snip>
>Does that clarify things?
>
>The reason why this is counterintuitive is that macros give the 
>illusion that function definitions and macro definitions are 
>evaluated in the same "world".  Although the same language is used, 
>there are really two worlds: the run time world and the expansion 
>time world.  This makes it hard to write macros that need to share 
>some expansion time function or state.  Here's one way to achieve 
>this.
>
>(define-macro (at-expand-time expr) (eval expr) `(begin))
>
>(at-expand-time (define (foo x) x))
>(define-macro (bar x) `(+ ,x ,(foo x)))
>(define (baz x) (bar x))

Yes, thank you. It is interesting.

I see that macros are removed from the runtime state. If you add a 
REPL in the file, (foo 3) and (baz 3) work but (bar 3) fails. The 
"side-effect" of the at-expand-time macro survived but not the macros 
themselves.


-- 
Stephane!
coleste at videotron.ca (Stephane Le Cornec)
+------------------- Made with recycled electrons. --------------------+
| #include <disclaimer.h>                             Kebekkujin desu. |
+---------<http://www.starfiredesign.com/starfire/index.html>----------+



More information about the Gambit-list mailing list