Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

understanding macros in lisp

8 views
Skip to first unread message

agent smith

unread,
Jun 28, 2003, 7:17:41 PM6/28/03
to
Lets say i have the following code.

(defmacro test (x)
(format t "x = ~A ~%" x))

If i understand macros correctly, if you have a call like (test (f a)),
x gets bound to the expression (f a) and not the value of it. In other
words if test was a function rather than a macro and f was not a defined
function, then the above call wouldn't go through because it would try
to evaluate (f a) which would obviosuly fail. I tried this case and it
failed exactly as i expected it to.

So far so good. But where i am confused is that when I call (test (f
a)), where test is the above defined macro, the output i get is "x = (f
a)". But i expected it to fail b/c in the call to format, x which is
bound to (f a) should be treated like code not data, and hence
evaluated. But evaluation of (f a) should fail since f is not a defined
function. But why is this not happening?

Could someone explain this. I am sure that I am missing something very
fundamental about macros here?

Thanx.

Kent M Pitman

unread,
Jun 28, 2003, 7:25:26 PM6/28/03
to
agent smith <worldneed...@yahoo.com> writes:

> Lets say i have the following code.
>
> (defmacro test (x)
> (format t "x = ~A ~%" x))

This macro does I/o at expansion time and returns NIL as its expansion.

Yes, very fundamental.

It's doing the FORMAT at macroexpand time (at compilation time) not
at code execution time. In interpreted code, macros are expanded at
execution time, so you are confusing yourself.

If you put
(defun foo () (test (f a)))
in a file and compile it, you will see the output at file compilation
time and no effect of FOO at runtime because FORMAT returns NIL and
NIL is the result of the macro expansion.

What you want to do is:
(defmacro test (x)
`(format t "x = ~A ~%" ,x))

if you want the expansion of the macro to be (format t "x = ~A" (f a))
and

(defmacro test (x)
`(format t "x = ~A ~%" ',x))

if you want the expansion to be (format t "x = ~A" '(f a)).

Edi Weitz

unread,
Jun 28, 2003, 7:58:15 PM6/28/03
to
agent smith <worldneed...@yahoo.com> writes:

You might want to read Drew McDermott's text at

<http://cl-cookbook.sourceforge.net/macros.html>

Edi.

Pascal Costanza

unread,
Jun 28, 2003, 7:59:00 PM6/28/03
to
In article <bdl7mv$8do$1...@news-int.gatech.edu>,
agent smith <worldneed...@yahoo.com> wrote:

> Could someone explain this. I am sure that I am missing something very
> fundamental about macros here?
>

One of the best introductions to macros is Chapter 7 of Paul Graham's
"On Lisp" that can be downloaded at http://www.paulgraham.com/onlisp.html

Read it! ;)


Pascal

Rob Warnock

unread,
Jun 29, 2003, 5:16:45 AM6/29/03
to
Kent M Pitman <pit...@world.std.com> wrote:
+---------------

| agent smith <worldneed...@yahoo.com> writes:
| > (defmacro test (x)
| > (format t "x = ~A ~%" x))
...

| What you want to do is:
| (defmacro test (x)
| `(format t "x = ~A ~%" ,x))
| if you want the expansion of the macro to be (format t "x = ~A" (f a)) and
| (defmacro test (x)
| `(format t "x = ~A ~%" ',x))
| if you want the expansion to be (format t "x = ~A" '(f a)).
+---------------

Actually -- just guessing, of course, mindreading a confused question
is always an iffy thing at best ;-} -- I suspect that maybe what he
*really* wants is something like this:

> (defmacro test (x)
`(format t "~S = ~S~%" ',x ,x))
> (test (+ 17 (* 4 3)))
(+ 17 (* 4 3)) = 29
NIL
>


-Rob

p.s. I sometimes use a little macro DBGV (for "debug values") that
takes an optional place and a list of forms and does the above on them.
Useful for "printf"-style debugging, e.g.:

> (let ((x 'this)
(y "that"))
(dbgv (top-level)
x y (+ 17 (* 4 3)) *package*
(get-dispatch-macro-character #\# #\!)))
DBGV: @TOP-LEVEL:
X = THIS
Y = "that"
(+ 17 (* 4 3)) = 29
*PACKAGE* = #<The COMMON-LISP-USER package, 91/95 internal, 0/9 external>
(GET-DISPATCH-MACRO-CHARACTER # !) = #<Function ORG.RPW3.UTILS::SHARP-BANG
{480416C1}>
NIL
>

Rob Warnock, PP-ASEL-IA <rp...@rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607

Mark Conrad

unread,
Jun 29, 2003, 4:06:04 PM6/29/03
to
In article <costanza-250FC2...@news.netcologne.de>, Pascal
Costanza <cost...@web.de> wrote:

> One of the best introductions to macros is Chapter 7 of Paul Graham's

> "On Lisp" that can be downloaded...<snipped>...

Are the rumors correct that an updated version of that book is in
prospect?

Thanks...

Mark-

Edi Weitz

unread,
Jun 29, 2003, 4:12:46 PM6/29/03
to
Mark Conrad <nos...@iam.invalid> writes:

<http://www.google.com/groups?selm=add24301.0306271142.2b67e898%40posting.google.com>

They're not talking about an "updated" version, though. You might want
to ask them directly. Apress is at <http://www.apress.com/>.

Edi.

0 new messages