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

79 lines
4.5 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>5.1.4. 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=tex2html2263 HREF="node61.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html2261 HREF="node56.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html2255 HREF="node59.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html2265 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html2266 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html2264 HREF="node61.html"> Function Calls</A>
<B>Up:</B> <A NAME=tex2html2262 HREF="node56.html"> Forms</A>
<B> Previous:</B> <A NAME=tex2html2256 HREF="node59.html"> Special Forms</A>
<HR> <P>
<H2><A NAME=SECTION00914000000000000000>5.1.4. Macros</A></H2>
<P>
If a form is a list and the first element is not the name of a special
form, it may be the name of a <i>macro</i>; if so, the form is said
to be a <i>macro call</i>. A macro is essentially a function from
forms to forms that will, given a call to that macro, compute
a new form to be evaluated in place of the macro call.
(This computation is sometimes referred to as <i>macro expansion</i>.)
For example, the macro named <tt>return</tt> will take a form such as
<tt>(return x)</tt> and from that form compute a new form
<tt>(return-from <tt>nil</tt> x)</tt>. We say that the old
form <i>expands</i> into the new form. The new form is then evaluated in
place of the original form; the value of the new form is returned as the
value of the original form.
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
X3J13 voted in January 1989
(DOTTED-MACRO-FORMS) <A NAME=3136>&#160;</A>
to clarify that macro calls, and subforms
of macro calls, need not be proper lists, but that use of dotted forms
requires the macro definition to use ``<tt>. <i>var</i></tt>'' or
``<tt>&amp;rest <i>var</i></tt>'' in order to match them properly.
It is then the responsibility of the macro definition to recognize
and appropriately handle such dotted forms or subforms.
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
There are a number of standard macros in Common Lisp, and the user can define more
by using <tt>defmacro</tt>.
<P>
Macros provided by a Common Lisp implementation as described herein may expand
into code that is not portable among differing implementations.
That is, a macro call may be implementation-independent because
the macro is defined in this book, but the expansion need not be.
<P>
<hr>
<b>Implementation note:</b> Implementors are encouraged to implement the macros
defined in this book, as far as is possible, in such a way that
the expansion will not contain any implementation-dependent
special forms, nor contain as forms data objects that
are not considered to be forms in Common Lisp.
The purpose of this restriction is to ensure that the expansion
can be processed by a program-analyzing program in an
implementation-independent manner.
There is no problem with a macro expansion containing
calls to implementation-dependent functions.
This restriction is not a requirement of Common Lisp; it is recognized
that certain complex macros may be able to expand into significantly
more efficient code in certain implementations
by using implementation-dependent special forms in the macro expansion.
<hr>
<P>
<BR> <HR><A NAME=tex2html2263 HREF="node61.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html2261 HREF="node56.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html2255 HREF="node59.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html2265 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html2266 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html2264 HREF="node61.html"> Function Calls</A>
<B>Up:</B> <A NAME=tex2html2262 HREF="node56.html"> Forms</A>
<B> Previous:</B> <A NAME=tex2html2256 HREF="node59.html"> Special Forms</A>
<HR> <P>
<HR>
<P><ADDRESS>
AI.Repository@cs.cmu.edu
</ADDRESS>
</BODY>