Reputation: 4861
This is a quotation on the topic "compilation and macro expansion", from the book Common Lisp: A gentle introduction to symbolic computation.
(defmacro bad-announce-macro ()
(format t "~%Hi mom!"))
(defun say-hi ()
(bad-announce-macro))
> (compile 'say-hi)
Hi, mom!
SAY-HI
> (say-hi)
NIL
In the above example the macro was expanded as part of the process of
compiling SAY-HI
. So the compiler said ‘‘Hi, mom!’’
The result of the
macro was NIL
, so that’s what got compiled into the body of SAY-HI
. When
we call the compiled SAY-HI
function, it says nothing because the macro has
been replaced with its expansion.
In this quotation, the author says that
the macro has been replaced with its expansion
Okay, so shouldn't it atleast show the printed "Hi mom!"
? Because though the macro doesn't return anything, but it still replaced by something (it's expansion). Based on the code, I hypothesized that
when a function is called after it is compiled, all the macros, that are called in it's body, are expanded into the result they return , not into whatever they have in their own bodies.
I'm not sure if this is right. And the reason for doing this is also not clear.
Upvotes: 0
Views: 78
Reputation: 139241
How often a macro is expanded is unspecified.
Interpreted Lisp in LispWorks:
CL-USER 49 > (say-hi)
Hi mom!
Hi mom!
NIL
The macro expansion is done twice at runtime here.
In compiled code we expect that no macro expansion is necessary at runtime. Thus in your example nothing will be printed, since your generated code does nothing:
CL-USER 50 > (compile 'say-hi)
Hi mom!
SAY-HI
NIL
NIL
CL-USER 51 > (say-hi)
NIL
The macro expansion is NIL
.
CL-USER 52 > (macroexpand '(bad-announce-macro))
Hi mom!
NIL ; <- the macro expansion
T
Upvotes: 2