From: agent smith
Subject: understanding macros in lisp
Date: 
Message-ID: <bdl7mv$8do$1@news-int.gatech.edu>
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.

From: Kent M Pitman
Subject: Re: understanding macros in lisp
Date: 
Message-ID: <sfw3chtu6mx.fsf@shell01.TheWorld.com>
agent smith <··················@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.

> 
> 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?

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)).
From: Rob Warnock
Subject: Re: understanding macros in lisp
Date: 
Message-ID: <GyednRyX9NzgM2OjXTWc-g@speakeasy.net>
Kent M Pitman  <······@world.std.com> wrote:
+---------------
| agent smith <··················@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		<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Edi Weitz
Subject: Re: understanding macros in lisp
Date: 
Message-ID: <87isqphi08.fsf@bird.agharta.de>
agent smith <··················@yahoo.com> writes:

> 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?

You might want to read Drew McDermott's text at

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

Edi.
From: Pascal Costanza
Subject: Re: understanding macros in lisp
Date: 
Message-ID: <costanza-250FC2.01590029062003@news.netcologne.de>
In article <············@news-int.gatech.edu>,
 agent smith <··················@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
From: Mark Conrad
Subject: Re: understanding macros in lisp
Date: 
Message-ID: <290620031305467905%nospam@iam.invalid>
In article <······························@news.netcologne.de>, Pascal
Costanza <········@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-
From: Edi Weitz
Subject: Re: understanding macros in lisp
Date: 
Message-ID: <877k74tzgh.fsf@bird.agharta.de>
Mark Conrad <······@iam.invalid> writes:

> In article <······························@news.netcologne.de>, Pascal
> Costanza <········@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?

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