[gambit-list] Using a function in a macro

Matthew Koichi Grimes mkg at cs.nyu.edu
Sat Jun 18 12:13:31 EDT 2011


You want:

(define-macro (macro-inc x) (list 'inc x))

(list 'inc x) evaluates at macro-expansion-time as (inc x), which at runtime
evaluates to what you want.

The code you wrote does work when entered in interactive mode (gsi); I seem
to recall there being a thread in the past month or two about why that might
be. I think the gist was that when running non-interactively, gsc does
separate passes for macro-expansion, then evaluation. In the macro-expansion
pass, it doesn't yet know about functions defined in that file, so calling
those functions directly like you did results in an error. On the other
hand, in interactive mode, each line gets its own macro-expansion and
evaluation pass. So by the time we get to your (define two (macro-inc 1))
line, the macro-expansion pass recognizes 'inc', defined (in the evaluation
pass) of an earlier line.

I hope I got that right,
-- Matt

On Sat, Jun 18, 2011 at 8:11 AM, Benjohn Barnes <benjohn at fysh.org> wrote:

>
> Hi,
>
> I think I must not be understanding something basic about macro expansion.
>
> I've reduced a more complex failure with macros that I am having to this
> simple example:
>
> > ; Define a function.
> > (define (inc x) (+ 1 x))
> >
> > ;Define a macro using the function.
> > (define-macro (macro-inc x) (inc x))
> >
> > ; This works okay.
> > (define two (inc 1))
> >
> > ; This fails on load with:
> > ; *** ERROR IN #<procedure #2>, "macro-test.scm"@5.30 -- Unbound
> variable: inc
> > (define macro-two (macro-inc 1))
>
> If I try to load this in to gsc (or if I import in to bsc), then when I try
> and use (expand) macro-inc (as shown in the last line), I get an error. It's
> as if I'm not allowed to use functions I have defined in the expansion of a
> macro?
>
> Thanks,
>        Benjohn
>
> --
> benjohn at fysh.org - Twitter @benjohnbarnes - Skype benjohnbarnes - Mobile +44
> (0) 7968 851 636
>
> _______________________________________________
> Gambit-list mailing list
> Gambit-list at iro.umontreal.ca
> https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.iro.umontreal.ca/pipermail/gambit-list/attachments/20110618/c60fca90/attachment.htm>


More information about the Gambit-list mailing list