[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Macro vs Function

From: David Kastrup
Subject: Re: Macro vs Function
Date: Wed, 12 Apr 2006 13:19:17 +0200
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux)

"Herbert Euler" <address@hidden> writes:

>>From: David Kastrup <address@hidden>
>>To: "Herbert Euler" <address@hidden>
>>CC: address@hidden
>>Subject: Re: Macro vs Function
>>Date: Wed, 12 Apr 2006 11:53:25 +0200
>>(defmacro xxx (a) a)
>>(defun yyy (a) a)
>>(xxx (+ 3 4)) => 7
>>(yyy '(+ 3 4)) => (+ 3 4)
> I'm confused now.  Take a look at this:
>    (defun f ()
>      nil)
>    (defmacro m (a)
>      (if a
>          t
>        nil))
>    (m (f)) => t
> Why does (f) in this example not get evaluated?

Uh, why should it get evaluated?  Macro arguments are _never_
evaluated.  They are passed quoted into the macro, the macro gets
executed, and the return value of the macro gets evaluated.

In the above, m is called with '(f) as its argument.  (if '(a)
... obviously evaluates to t which the macro returns.  Then t gets
evaluated, but since it is a self-quoting form, nothing else happens
with it.

Here is a more complex example:

(defmacro m (a)
  (append a '(2 4)))

(m (+)) => 6

And here is what happens in compilation:

(defun xxx () (m (or)))

M-x disassemble RET xxx RET

byte code for xxx:
  args: nil
0       constant  2
1       return    

David Kastrup, Kriemhildstr. 15, 44793 Bochum

reply via email to

[Prev in Thread] Current Thread [Next in Thread]