116 lines
7.7 KiB
HTML
116 lines
7.7 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: Special Operator LET, LET*</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="m_destru.htm">
|
||
|
<LINK REL=UP HREF="c_data_a.htm">
|
||
|
<LINK REL=NEXT HREF="s_progv.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="m_destru.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="c_data_a.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="s_progv.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
|
||
|
|
||
|
<HR>
|
||
|
|
||
|
<A NAME="let"><A NAME="letST"><I>Special Operator</I> <B>LET, LET*</B></A></A> <P>
|
||
|
<P>
|
||
|
<P><B>Syntax:</B><P>
|
||
|
<P>
|
||
|
|
||
|
<B>let</B> <I>({<I>var</I> | (<I>var</I> [<I>init-form</I>])}*) <I>declaration</I><B>*</B> <I>form</I><B>*</B></I> => <I><I>result</I><B>*</B></I><P>
|
||
|
<P>
|
||
|
|
||
|
<B>let*</B> <I>({<I>var</I> | (<I>var</I> [<I>init-form</I>])}*) <I>declaration</I><B>*</B> <I>form</I><B>*</B></I> => <I><I>result</I><B>*</B></I><P>
|
||
|
<P>
|
||
|
<P><B>Arguments and Values:</B><P>
|
||
|
<P>
|
||
|
<I>var</I>---a <A REL=DEFINITION HREF="26_glo_s.htm#symbol"><I>symbol</I></A>. <P>
|
||
|
<I>init-form</I>---a <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>. <P>
|
||
|
<I>declaration</I>---a <A REL=DEFINITION HREF="s_declar.htm#declare"><B>declare</B></A> <A REL=DEFINITION HREF="26_glo_e.htm#expression"><I>expression</I></A>; not evaluated. <P>
|
||
|
<I>form</I>---a <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>. <P>
|
||
|
<I>results</I>---the <A REL=DEFINITION HREF="26_glo_v.htm#value"><I>values</I></A> returned by the <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>forms</I></A>. <P>
|
||
|
<P><B>Description:</B><P>
|
||
|
<P>
|
||
|
<A REL=DEFINITION HREF="#let"><B>let</B></A> and <A REL=DEFINITION HREF="#letST"><B>let*</B></A> create new variable <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>bindings</I></A> and execute a series of <I>forms</I> that use these <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>bindings</I></A>. <A REL=DEFINITION HREF="#let"><B>let</B></A> performs the <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>bindings</I></A> in parallel and <A REL=DEFINITION HREF="#letST"><B>let*</B></A> does them sequentially. <P>
|
||
|
The form <P>
|
||
|
|
||
|
<PRE>
|
||
|
(let ((var1 init-form-1)
|
||
|
(var2 init-form-2)
|
||
|
...
|
||
|
(varm init-form-m))
|
||
|
declaration1
|
||
|
declaration2
|
||
|
...
|
||
|
declarationp
|
||
|
form1
|
||
|
form2
|
||
|
...
|
||
|
formn)
|
||
|
</PRE>
|
||
|
</TT> first evaluates the expressions <I>init-form-1</I>, <I>init-form-2</I>, and so on, in that order, saving the resulting values. Then all of the variables <I>varj</I> are bound to the corresponding values; each <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>binding</I></A> is lexical unless there is a <A REL=DEFINITION HREF="d_specia.htm#special"><B>special</B></A> declaration to the contrary. The expressions <I>formk</I> are then evaluated in order; the values of all but the last are discarded (that is, the body of a <A REL=DEFINITION HREF="#let"><B>let</B></A> is an <A REL=DEFINITION HREF="26_glo_i.htm#implicit_progn"><I>implicit progn</I></A>). <P>
|
||
|
<A REL=DEFINITION HREF="#letST"><B>let*</B></A> is similar to <A REL=DEFINITION HREF="#let"><B>let</B></A>, but the <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>bindings</I></A> of variables are performed sequentially rather than in parallel. The expression for the <I>init-form</I> of a <I>var</I> can refer to <I>vars</I> previously bound in the <A REL=DEFINITION HREF="#letST"><B>let*</B></A>. <P>
|
||
|
The form <P>
|
||
|
|
||
|
<PRE>
|
||
|
(let* ((var1 init-form-1)
|
||
|
(var2 init-form-2)
|
||
|
...
|
||
|
(varm init-form-m))
|
||
|
declaration1
|
||
|
declaration2
|
||
|
...
|
||
|
declarationp
|
||
|
form1
|
||
|
form2
|
||
|
...
|
||
|
formn)
|
||
|
</PRE>
|
||
|
</TT> first evaluates the expression <I>init-form-1</I>, then binds the variable <I>var1</I> to that value; then it evaluates <I>init-form-2</I> and binds <I>var2</I>, and so on. The expressions <I>formj</I> are then evaluated in order; the values of all but the last are discarded (that is, the body of <A REL=DEFINITION HREF="#letST"><B>let*</B></A> is an implicit <A REL=DEFINITION HREF="s_progn.htm#progn"><B>progn</B></A>). <P>
|
||
|
For both <A REL=DEFINITION HREF="#let"><B>let</B></A> and <A REL=DEFINITION HREF="#letST"><B>let*</B></A>, if there is not an <I>init-form</I> associated with a <I>var</I>, <I>var</I> is initialized to <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>. <P>
|
||
|
The special form <A REL=DEFINITION HREF="#let"><B>let</B></A> has the property that the <A REL=DEFINITION HREF="26_glo_s.htm#scope"><I>scope</I></A> of the name binding does not include any initial value form. For <A REL=DEFINITION HREF="#letST"><B>let*</B></A>, a variable's <A REL=DEFINITION HREF="26_glo_s.htm#scope"><I>scope</I></A> also includes the remaining initial value forms for subsequent variable bindings. <P>
|
||
|
<P><B>Examples:</B><P>
|
||
|
<P>
|
||
|
<PRE>
|
||
|
(setq a 'top) => TOP
|
||
|
(defun dummy-function () a) => DUMMY-FUNCTION
|
||
|
(let ((a 'inside) (b a))
|
||
|
(format nil "~S ~S ~S" a b (dummy-function))) => "INSIDE TOP TOP"
|
||
|
(let* ((a 'inside) (b a))
|
||
|
(format nil "~S ~S ~S" a b (dummy-function))) => "INSIDE INSIDE TOP"
|
||
|
(let ((a 'inside) (b a))
|
||
|
(declare (special a))
|
||
|
(format nil "~S ~S ~S" a b (dummy-function))) => "INSIDE TOP INSIDE"
|
||
|
</PRE>
|
||
|
</TT> <P>
|
||
|
The code <P>
|
||
|
<PRE>
|
||
|
(let (x)
|
||
|
(declare (integer x))
|
||
|
(setq x (gcd y z))
|
||
|
...)
|
||
|
</PRE>
|
||
|
</TT> is incorrect; although <TT>x</TT> is indeed set before it is used, and is set to a value of the declared type <A REL=DEFINITION HREF="26_glo_i.htm#integer"><I>integer</I></A>, nevertheless <TT>x</TT> initially takes on the value <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A> in violation of the type declaration. <P>
|
||
|
<P><B>Affected By:</B> None.
|
||
|
<P>
|
||
|
<P><B>Exceptional Situations:</B> None.
|
||
|
<P>
|
||
|
<P><B>See Also:</B><P>
|
||
|
<P>
|
||
|
<A REL=DEFINITION HREF="s_progv.htm#progv"><B>progv</B></A> <P>
|
||
|
<P><B>Notes:</B> None.
|
||
|
<P>
|
||
|
<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/iss358.htm">VARIABLE-LIST-ASYMMETRY:SYMMETRIZE</A><LI> <A REL=CHILD HREF="../Issues/iss097.htm">DECLS-AND-DOC</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>
|