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

Marc Feeley feeley at iro.umontreal.ca
Thu May 11 17:23:14 EDT 2006


On 11-May-06, at 4:19 PM, Stephane Le Cornec wrote:

>>
>> (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.

If you want a macro definition to survive until runtime (so that it  
is visible to "eval" or the REPL), then you need to use something  
like this:

(define-macro (for-expand-time expr) (eval expr) '(begin))
(define-macro (for-run-time expr) expr)
(define-macro (for-both-times expr) `(begin (for-expand-time ,expr)  
(for-run-time ,expr)))

(for-both-times
   (define (foo x) x))

(for-both-times
   (define-macro (bar x) `(+ ,x ,(foo x))))

(for-both-times
   (define (baz x) (bar x)))

Marc



More information about the Gambit-list mailing list