[gambit-list] How to have define-macro expand to multiple

Adrien Piérard pierarda at iro.umontreal.ca
Sun Sep 13 23:18:13 EDT 2009

If you mean being able to do this

(define (fun x)
  (list x x))

(define-macro (mac arg)
  (let ((r (fun arg)))
    `(pretty-print ',r)))

(mac 42)

in the REPL, but not to compile it, it's normal.

When you eval the first define it in the REPL, gambit learns about FUN.
Then, the definition of macro knows it as you do eval it.
And when you apply the macro, it works fine.

First, macro expansion happens. It does not know FUN yet.
So, it fails, for you call a function that will be eval'd *after*
macro expansion!

How to do it?
It's a bit tricky, but you can try that (though other ways exist):

(define-macro (start-macro)
  ;; macro environment learns about FUN
  (eval '(define (fun x)
           (list x x))))

(define-macro (mac arg)
  ;; what was DEFINED in a macro is shared by all macros at this stage
  ;; But it's only accessible through EVAL.
  (let ((r (eval `(fun ,arg))))
    `(pretty-print ',r)))

;; Call the macro, to add FUN to the macro env

;; When this is expanded (at macro expansion time), it will know about FUN
;; which has been added to the macro environment, at macro expansion time,
;; on the previous line.
(mac 42)

Does this help you?


2009/9/14 Isaac Freeman <memotype at gmail.com>:
> So, I have that much working... Is there a way to modularize
> DEFINE-MACRO code? When in the gsi REPL, I can define a function, and
> then call that from the macro, but when I try that in a file and try
> to compile that, it complains that the function isn't defined... am I
> missing something?
> Thanks.
> On Sun, Sep 13, 2009 at 11:42 AM, Ben Weaver <ben at orangesoda.net> wrote:
>> Hi Isaac,
>> On Sun, Sep 13, 2009 at 11:24 AM, Isaac Freeman <memotype at gmail.com> wrote:
>>> Specifically I'm looking to expand a macro to
>>> multiple, distinct, top-level (define)'s.
>> Try expanding into a BEGIN:
>> (begin
>>  (define ...)
>>  (define ...)
>>  ...)
>> Good luck!
>> -Ben
> --
> Isaac Freeman
> memotype (at) gmail.com
> "The diversity of mankind is a basic postulate of our knowledge of
> human beings. But if mankind is diverse and individuated, then how can
> anyone propose equality as an ideal? Every year, scholars hold
> Conferences on Equality and call for greater equality, and no one
> challenges the basic tenet. But what justification can equality find
> in the nature of man? If each individual is unique, how else can he be
> made 'equal' to others than by destroying most of what is human in him
> and reducing human society to the mindless uniformity of the ant
> heap?" --Murray N. Rothbard
> _______________________________________________
> Gambit-list mailing list
> Gambit-list at iro.umontreal.ca
> https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list

Français, English, 日本語, 한국어

More information about the Gambit-list mailing list