emacs.d/clones/www.cs.cmu.edu/Groups/AI/html/cltl/clm/node97.html

88 lines
5.2 KiB
HTML
Raw Normal View History

2022-08-26 19:11:35 +02:00
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
<!Converted with LaTeX2HTML 0.6.5 (Tue Nov 15 1994) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
<HEAD>
<TITLE>8. Macros</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Macros">
<meta name="keywords" value="clm">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<b>Common Lisp the Language, 2nd Edition</b>
<BR> <HR><A NAME=tex2html2729 HREF="node98.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html2727 HREF="clm.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html2721 HREF="node96.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html2731 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html2732 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html2730 HREF="node98.html"> Macro Definition</A>
<B>Up:</B> <A NAME=tex2html2728 HREF="clm.html">Common Lisp the Language</A>
<B> Previous:</B> <A NAME=tex2html2722 HREF="node96.html"> Dynamic Non-Local Exits</A>
<HR> <P>
<H1><A NAME=SECTION001200000000000000000>8. Macros</A></H1>
<P>
<A NAME=MACROS>The</A>
Common Lisp macro facility allows the user to define arbitrary
functions that convert certain Lisp forms into different forms before
evaluating or compiling them. This is done at the expression level,
not at the character-string level as in most other languages. Macros
are important in the writing of good code: they make it possible to
write code that is clear and elegant at the user level but that is
converted to a more complex or more efficient internal form for
execution.
<P>
When <tt>eval</tt> is given a list whose <i>car</i> is a symbol, it looks
for local definitions of that symbol (by <tt>flet</tt>, <tt>labels</tt>,
and <tt>macrolet</tt>); if that fails, it looks for a global definition.
If the definition is a macro definition, then the original
list is said to be a <i>macro call</i>. Associated with the definition
will be a function of two arguments, called the <i>expansion function</i>.
This function is called with the entire macro call as its first argument
(the second argument is a lexical environment);
it must return some new Lisp form, called the <i>expansion</i> of the
macro call. (Actually, a more general mechanism is involved;
see <tt>macroexpand</tt>.)
This expansion is then evaluated in place of the original
form.
<P>
When a function is being compiled, any macros it contains are expanded
at compilation time. This means that a macro definition must be seen by the
compiler before the first use of the macro.
<P>
More generally, an implementation of Common Lisp has great latitude in deciding
exactly when to expand macro calls within a program. For example,
it is acceptable for the <tt>defun</tt> special form to expand all macro
calls within its body at the time the <tt>defun</tt> form is executed
and record the fully expanded body as the body of the function
being defined.
(An implementation might even choose always to compile functions defined
by <tt>defun</tt>, even when operating in an ``interpretive'' mode.)
<P>
Macros should be written so as to depend as little as possible
on the execution environment to produce a correct expansion. To ensure
consistent behavior, it is best to ensure that all macro definitions are
available, whether to the interpreter or compiler, before any code
containing calls to those macros is introduced.
<P>
In Common Lisp, macros are not functions.
In particular, macros cannot be used as
functional arguments to such functions as <tt>apply</tt>, <tt>funcall</tt>,
or <tt>map</tt>; in such situations, the list representing the ``original macro
call'' does not exist, and cannot exist, because in some sense the arguments
have already been evaluated.
<P>
<HR>
<UL>
<LI> <A NAME=tex2html2733 HREF="node98.html#SECTION001210000000000000000"> Macro Definition</A>
<LI> <A NAME=tex2html2734 HREF="node99.html#SECTION001220000000000000000"> Macro Expansion</A>
<LI> <A NAME=tex2html2735 HREF="node100.html#SECTION001230000000000000000"> Destructuring</A>
<LI> <A NAME=tex2html2736 HREF="node101.html#SECTION001240000000000000000"> Compiler Macros</A>
<LI> <A NAME=tex2html2737 HREF="node102.html#SECTION001250000000000000000"> Environments</A>
</UL>
<BR> <HR><A NAME=tex2html2729 HREF="node98.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html2727 HREF="clm.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html2721 HREF="node96.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html2731 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html2732 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html2730 HREF="node98.html"> Macro Definition</A>
<B>Up:</B> <A NAME=tex2html2728 HREF="clm.html">Common Lisp the Language</A>
<B> Previous:</B> <A NAME=tex2html2722 HREF="node96.html"> Dynamic Non-Local Exits</A>
<HR> <P>
<HR>
<P><ADDRESS>
AI.Repository@cs.cmu.edu
</ADDRESS>
</BODY>