1
0
Fork 0
cl-sites/HyperSpec-7-0/HyperSpec/Body/03_dd.htm
2024-04-01 10:24:07 +02:00

84 lines
12 KiB
HTML

<!-- Common Lisp HyperSpec (TM), version 7.0 generated by Kent M. Pitman on Mon, 11-Apr-2005 2:31am EDT -->
<HTML>
<HEAD>
<TITLE>CLHS: Section 3.4.4</TITLE>
<LINK HREF="../Data/clhs.css" REL="stylesheet" TYPE="text/css" />
<META HTTP-EQUIV="Author" CONTENT="Kent M. Pitman">
<META HTTP-EQUIV="Organization" CONTENT="LispWorks Ltd.">
<LINK REL=TOP HREF="../Front/index.htm">
<LINK REL=COPYRIGHT HREF="../Front/Help.htm#Legal">
<LINK REL=DISCLAIMER HREF="../Front/Help.htm#Disclaimer">
<LINK REL=PREV HREF="03_dc.htm">
<LINK REL=UP HREF="03_d.htm">
<LINK REL=NEXT HREF="03_dda.htm">
</HEAD>
<BODY>
<H1><A REV=MADE HREF="http://www.lispworks.com/"><IMG WIDTH=80 HEIGHT=65 ALT="[LISPWORKS]" SRC="../Graphics/LWSmall.gif" ALIGN=Bottom></A><A REL=TOP HREF="../Front/index.htm"><IMG WIDTH=237 HEIGHT=65 ALT="[Common Lisp HyperSpec (TM)]" SRC="../Graphics/CLHS_Sm.gif" ALIGN=Bottom></A> <A REL=PREV HREF="03_dc.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="03_d.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="03_dda.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
<HR>
<H2>
3.4.4 Macro Lambda Lists</H2> <A NAME="AMbody"></A> <A NAME="AMwhole"></A> <A NAME="AMenvironment"></A> <P>
A <A REL=DEFINITION HREF="26_glo_m.htm#macro_lambda_list"><I>macro lambda list</I></A> is used in describing <A REL=DEFINITION HREF="26_glo_m.htm#macro"><I>macros</I></A> defined by the <A REL=DEFINITION HREF="26_glo_o.htm#operator"><I>operators</I></A> in the next figure. <P>
<PRE>
<A REL=DEFINITION HREF="m_define.htm#define-compiler-macro">define-compiler-macro</A> <A REL=DEFINITION HREF="m_defmac.htm#defmacro">defmacro</A> <A REL=DEFINITION HREF="s_flet_.htm#macrolet">macrolet</A>
<A REL=DEFINITION HREF="m_defi_3.htm#define-setf-expander">define-setf-expander</A>
</PRE>
<P><B>Figure 3-17. Operators that use Macro Lambda Lists</B> <P>
With the additional restriction that an <A REL=DEFINITION HREF="26_glo_e.htm#environment_parameter"><I>environment parameter</I></A> may appear only once (at any of the positions indicated), a <A REL=DEFINITION HREF="26_glo_m.htm#macro_lambda_list"><I>macro lambda list</I></A> has the following syntax: <P>
<P> <PRE>
reqvars::= var*
</PRE>
<PRE>
optvars::= [&amp;optional {var | (var [init-form [supplied-p-parameter]])}*]
</PRE>
<PRE>
restvar::= [{&amp;rest | &amp;body} var]
</PRE>
<PRE>
keyvars::= [&amp;key {var | ({var | (keyword-name var)} [init-form [supplied-p-parameter]])}*
[&amp;allow-other-keys]]
</PRE>
<PRE>
auxvars::= [&amp;aux {var | (var [init-form])}*]
</PRE>
<PRE>
envvar::= [&amp;environment var]
</PRE>
<PRE>
wholevar::= [&amp;whole var]
</PRE>
<PRE>
lambda-list::= (wholevar envvar reqvars envvar optvars envvar
restvar envvar keyvars envvar auxvars envvar) |
(wholevar envvar reqvars envvar optvars envvar . var)
</PRE>
<PRE>
pattern::= (wholevar reqvars optvars restvar keyvars auxvars) |
(wholevar reqvars optvars . var)
</PRE>
<P> <P>
A <A REL=DEFINITION HREF="26_glo_m.htm#macro_lambda_list"><I>macro lambda list</I></A> can contain the <A REL=DEFINITION HREF="26_glo_l.htm#lambda_list_keyword"><I>lambda list keywords</I></A> shown in the next figure. <P>
<PRE>
&amp;allow-other-keys &amp;environment &amp;rest
&amp;aux &amp;key &amp;whole
&amp;body &amp;optional
</PRE>
<P><B>Figure 3-18. Lambda List Keywords used by Macro Lambda Lists</B> <P>
<A REL=DEFINITION HREF="26_glo_o.htm#optional_parameter"><I>Optional parameters</I></A> (introduced by <TT>&amp;optional</TT>) and <A REL=DEFINITION HREF="26_glo_k.htm#keyword_parameter"><I>keyword parameters</I></A> (introduced by <TT>&amp;key</TT>) can be supplied in a <A REL=DEFINITION HREF="26_glo_m.htm#macro_lambda_list"><I>macro lambda list</I></A>, just as in an <A REL=DEFINITION HREF="26_glo_o.htm#ordinary_lambda_list"><I>ordinary lambda list</I></A>. Both may contain default initialization forms and <A REL=DEFINITION HREF="26_glo_s.htm#supplied-p_parameter"><I>supplied-p parameters</I></A>. <P>
<TT>&amp;body</TT> is identical in function to <TT>&amp;rest</TT>, but it can be used to inform certain output-formatting and editing functions that the remainder of the <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A> is treated as a body, and should be indented accordingly. Only one of <TT>&amp;body</TT> or <TT>&amp;rest</TT> can be used at any particular level; see <A REL=CHILD HREF="03_dda.htm">Section 3.4.4.1 (Destructuring by Lambda Lists)</A>. <TT>&amp;body</TT> can appear at any level of a <A REL=DEFINITION HREF="26_glo_m.htm#macro_lambda_list"><I>macro lambda list</I></A>; for details, see <A REL=CHILD HREF="03_dda.htm">Section 3.4.4.1 (Destructuring by Lambda Lists)</A>. <P>
<TT>&amp;whole</TT> is followed by a single variable that is bound to the entire macro-call form; this is the value that the <A REL=DEFINITION HREF="26_glo_m.htm#macro_function"><I>macro function</I></A> receives as its first argument. If <TT>&amp;whole</TT> and a following variable appear, they must appear first in <I>lambda-list</I>, before any other parameter or <A REL=DEFINITION HREF="26_glo_l.htm#lambda_list_keyword"><I>lambda list keyword</I></A>. <TT>&amp;whole</TT> can appear at any level of a <A REL=DEFINITION HREF="26_glo_m.htm#macro_lambda_list"><I>macro lambda list</I></A>. At inner levels, the <TT>&amp;whole</TT> variable is bound to the corresponding part of the argument, as with <TT>&amp;rest</TT>, but unlike <TT>&amp;rest</TT>, other arguments are also allowed. The use of <TT>&amp;whole</TT> does not affect the pattern of arguments specified. <P>
<TT>&amp;environment</TT> is followed by a single variable that is bound to an <A REL=DEFINITION HREF="26_glo_e.htm#environment"><I>environment</I></A> representing the <A REL=DEFINITION HREF="26_glo_l.htm#lexical_environment"><I>lexical environment</I></A> in which the macro call is to be interpreted. This <A REL=DEFINITION HREF="26_glo_e.htm#environment"><I>environment</I></A> should be used with <A REL=DEFINITION HREF="f_macro_.htm#macro-function"><B>macro-function</B></A>, <A REL=DEFINITION HREF="f_get_se.htm#get-setf-expansion"><B>get-setf-expansion</B></A>, <A REL=DEFINITION HREF="f_cmp_ma.htm#compiler-macro-function"><B>compiler-macro-function</B></A>, and <A REL=DEFINITION HREF="f_mexp_.htm#macroexpand"><B>macroexpand</B></A> (for example) in computing the expansion of the macro, to ensure that any <A REL=DEFINITION HREF="26_glo_l.htm#lexical_binding"><I>lexical bindings</I></A> or definitions established in the <A REL=DEFINITION HREF="26_glo_c.htm#compilation_environment"><I>compilation environment</I></A> are taken into account. <TT>&amp;environment</TT> can only appear at the top level of a <A REL=DEFINITION HREF="26_glo_m.htm#macro_lambda_list"><I>macro lambda list</I></A>, and can only appear once, but can appear anywhere in that list; the <TT>&amp;environment</TT> <A REL=DEFINITION HREF="26_glo_p.htm#parameter"><I>parameter</I></A> is <A REL=DEFINITION HREF="26_glo_b.htm#bound"><I>bound</I></A> along with <TT>&amp;whole</TT> before any other <A REL=DEFINITION HREF="26_glo_v.htm#variable"><I>variables</I></A> in the <A REL=DEFINITION HREF="26_glo_l.htm#lambda_list"><I>lambda list</I></A>, regardless of where <TT>&amp;environment</TT> appears in the <A REL=DEFINITION HREF="26_glo_l.htm#lambda_list"><I>lambda list</I></A>. The <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> that is bound to the <A REL=DEFINITION HREF="26_glo_e.htm#environment_parameter"><I>environment parameter</I></A> has <A REL=DEFINITION HREF="26_glo_d.htm#dynamic_extent"><I>dynamic extent</I></A>. <P>
Destructuring allows a <A REL=DEFINITION HREF="26_glo_m.htm#macro_lambda_list"><I>macro lambda list</I></A> to express the structure of a macro call syntax. If no <A REL=DEFINITION HREF="26_glo_l.htm#lambda_list_keyword"><I>lambda list keywords</I></A> appear, then the <A REL=DEFINITION HREF="26_glo_m.htm#macro_lambda_list"><I>macro lambda list</I></A> is a <A REL=DEFINITION HREF="26_glo_t.htm#tree"><I>tree</I></A> containing parameter names at the leaves. The pattern and the <A REL=DEFINITION HREF="26_glo_m.htm#macro_form"><I>macro form</I></A> must have compatible <A REL=DEFINITION HREF="26_glo_t.htm#tree_structure"><I>tree structure</I></A>; that is, their <A REL=DEFINITION HREF="26_glo_t.htm#tree_structure"><I>tree structure</I></A> must be equivalent, or it must differ only in that some <A REL=DEFINITION HREF="26_glo_l.htm#leaf"><I>leaves</I></A> of the pattern match <A REL=DEFINITION HREF="26_glo_n.htm#non-atomic"><I>non-atomic</I></A> <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>objects</I></A> of the <A REL=DEFINITION HREF="26_glo_m.htm#macro_form"><I>macro form</I></A>. For information about error detection in this <A REL=DEFINITION HREF="26_glo_s.htm#situation"><I>situation</I></A>, see <A REL=CHILD HREF="03_eag.htm">Section 3.5.1.7 (Destructuring Mismatch)</A>. <P>
A destructuring <A REL=DEFINITION HREF="26_glo_l.htm#lambda_list"><I>lambda list</I></A> (whether at top level or embedded) can be dotted, ending in a parameter name. This situation is treated exactly as if the parameter name that ends the <A REL=DEFINITION HREF="26_glo_l.htm#list"><I>list</I></A> had appeared preceded by <TT>&amp;rest</TT>. <P>
It is permissible for a <A REL=DEFINITION HREF="26_glo_m.htm#macro"><I>macro</I></A> <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A> (or a <A REL=DEFINITION HREF="26_glo_s.htm#subexpression"><I>subexpression</I></A> of a <A REL=DEFINITION HREF="26_glo_m.htm#macro"><I>macro</I></A> <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>) to be a <A REL=DEFINITION HREF="26_glo_d.htm#dotted_list"><I>dotted list</I></A> only when <TT>(... &amp;rest var)</TT> or <TT>(... . var)</TT> is used to match it. It is the responsibility of the <A REL=DEFINITION HREF="26_glo_m.htm#macro"><I>macro</I></A> to recognize and deal with such situations. <P>
<A REL=CHILD HREF="03_dda.htm"><H2>
3.4.4.1 Destructuring by Lambda Lists</H2></A><P><P><HR>The following <A REL=META HREF="../Front/X3J13Iss.htm">X3J13 cleanup issues</A>, <I>not part of the specification</I>, apply to this section:<P><UL><LI> <A REL=CHILD HREF="../Issues/iss139.htm">DOTTED-MACRO-FORMS:ALLOW</A><LI> <A REL=CHILD HREF="../Issues/iss010.htm">ARGUMENT-MISMATCH-ERROR-AGAIN:CONSISTENT</A><LI> <A REL=CHILD HREF="../Issues/iss229.htm">MACRO-ENVIRONMENT-EXTENT:DYNAMIC</A><LI> <A REL=CHILD HREF="../Issues/iss001.htm">&amp;ENVIRONMENT-BINDING-ORDER:FIRST</A><LI> <A REL=CHILD HREF="../Issues/iss101.htm">DEFINE-COMPILER-MACRO:X3J13-NOV89</A><LI> <A REL=CHILD HREF="../Issues/iss342.htm">SYNTACTIC-ENVIRONMENT-ACCESS:RETRACTED-MAR91</A><LI> <A REL=CHILD HREF="../Issues/iss308.htm">SETF-METHOD-VS-SETF-METHOD:RENAME-OLD-TERMS</A><LI> <A REL=CHILD HREF="../Issues/iss185.htm">GET-SETF-METHOD-ENVIRONMENT:ADD-ARG</A><LI> <A REL=CHILD HREF="../Issues/iss231.htm">MACRO-FUNCTION-ENVIRONMENT:YES</A><LI> <A REL=CHILD HREF="../Issues/iss106.htm">DEFMACRO-LAMBDA-LIST:TIGHTEN-DESCRIPTION</A><P></UL><HR>
<A REL=NAVIGATOR HREF="../Front/StartPts.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Starting Points]" SRC="../Graphics/StartPts.gif" ALIGN=Bottom></A><A REL=TOC HREF="../Front/Contents.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Contents]" SRC="../Graphics/Contents.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Master.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Index]" SRC="../Graphics/Index.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Symbol.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Symbols]" SRC="../Graphics/Symbols.gif" ALIGN=Bottom></A><A REL=GLOSSARY HREF="../Body/26_a.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Glossary]" SRC="../Graphics/Glossary.gif" ALIGN=Bottom></A><A HREF="../Front/X3J13Iss.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Issues]" SRC="../Graphics/Issues.gif" ALIGN=Bottom></A><BR>
<A REL=COPYRIGHT HREF="../Front/Help.htm#Legal"><I>Copyright 1996-2005, LispWorks Ltd. All rights reserved.</I></A><P>
</BODY>
</HTML>