Struggling to grasp newlisp macros
Posted: Fri Sep 24, 2004 3:19 am
I'm trying to come up with a conceptual model of how
define-macro works.
>(define-macro (foo _x _x) (list _x _y))
(lambda-macro (_x _y) (list _x _y))
> (foo 1 2)
(1 2)
> (foo a b)
(a b)
So far so good. Conceptually I'm thinking the macro
invocation is a two step process.
> (foo a b)
((lambda-macro (_x _x) (list _x _y)) 'a 'b)
which returns
(list 'a 'b)
which is then interpreted normally returning
(a b)
But now when I define bar
> (define-macro (bar _x _y) (fn () (list _x _y))
(lambda-macro (_x _y) (lambda () (list _x _y)))
and apply it to
> (bar a b)
(lambda () (list _x _y))
But I expected from my obviously wrong conceptual
model for
> (bar a b)
> ((lambda-macro (_x _y) (lambda () (list _x _y)) 'a 'b)
to have returned
(lambda () (list 'a 'b))
But it returned instead
(lambda () (list _x _y))
It appears that the internally defined lambda expression
is evaluated without substituting 'a and 'b in place
of _x and _y formal arguments.
What's happening? Where is my thinking wrong?
Thanks
define-macro works.
>(define-macro (foo _x _x) (list _x _y))
(lambda-macro (_x _y) (list _x _y))
> (foo 1 2)
(1 2)
> (foo a b)
(a b)
So far so good. Conceptually I'm thinking the macro
invocation is a two step process.
> (foo a b)
((lambda-macro (_x _x) (list _x _y)) 'a 'b)
which returns
(list 'a 'b)
which is then interpreted normally returning
(a b)
But now when I define bar
> (define-macro (bar _x _y) (fn () (list _x _y))
(lambda-macro (_x _y) (lambda () (list _x _y)))
and apply it to
> (bar a b)
(lambda () (list _x _y))
But I expected from my obviously wrong conceptual
model for
> (bar a b)
> ((lambda-macro (_x _y) (lambda () (list _x _y)) 'a 'b)
to have returned
(lambda () (list 'a 'b))
But it returned instead
(lambda () (list _x _y))
It appears that the internally defined lambda expression
is evaluated without substituting 'a and 'b in place
of _x and _y formal arguments.
What's happening? Where is my thinking wrong?
Thanks