77 lines
6 KiB
HTML
77 lines
6 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 6.1.2.2</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="06_abaga.htm">
|
|
<LINK REL=UP HREF="06_ab.htm">
|
|
<LINK REL=NEXT HREF="06_abba.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="06_abaga.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="06_ab.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="06_abba.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
|
|
|
|
<HR>
|
|
|
|
<H2>
|
|
6.1.2.2 Local Variable Initializations</H2> <P>
|
|
When a <A REL=DEFINITION HREF="m_loop.htm#loop"><B>loop</B></A> <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A> is executed, the local variables are bound and are initialized to some value. These local variables exist until <A REL=DEFINITION HREF="m_loop.htm#loop"><B>loop</B></A> iteration terminates, at which point they cease to exist. Implicit variables are also established by iteration control clauses and the <TT>into</TT> preposition of accumulation clauses. <P>
|
|
The <TT>with</TT> construct initializes variables that are local to a loop. The variables are initialized one time only. If the optional <I>type-spec</I> argument is supplied for the variable <I>var</I>, but there is no related expression to be evaluated, <I>var</I> is initialized to an appropriate default value for its <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A>. For example, for the types <A REL=DEFINITION HREF="t_t.htm#t"><B>t</B></A>, <A REL=DEFINITION HREF="t_number.htm#number"><B>number</B></A>, and <A REL=DEFINITION HREF="t_float.htm#float"><B>float</B></A>, the default values are <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>, <TT>0</TT>, and <TT>0.0</TT> respectively. The consequences are undefined if a <I>type-spec</I> argument is supplied for <I>var</I> if the related expression returns a value that is not of the supplied <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A>. By default, the <TT>with</TT> construct initializes variables <A REL=DEFINITION HREF="26_glo_s.htm#sequentially"><I>sequentially</I></A>; that is, one variable is assigned a value before the next expression is evaluated. However, by using the <A REL=DEFINITION HREF="26_glo_l.htm#loop_keyword"><I>loop keyword</I></A> <TT>and</TT> to join several <TT>with</TT> clauses, initializations can be forced to occur in <A REL=DEFINITION HREF="26_glo_p.htm#parallel"><I>parallel</I></A>; that is, all of the supplied <I>forms</I> are evaluated, and the results are bound to the respective variables simultaneously. <P>
|
|
<A REL=DEFINITION HREF="26_glo_s.htm#sequential"><I>Sequential</I></A> <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>binding</I></A> is used when it is desireable for the initialization of some variables to depend on the values of previously bound variables. For example, suppose the variables <TT>a</TT>, <TT>b</TT>, and <TT>c</TT> are to be bound in sequence: <P>
|
|
<PRE>
|
|
(loop with a = 1
|
|
with b = (+ a 2)
|
|
with c = (+ b 3)
|
|
return (list a b c))
|
|
=> (1 3 6)
|
|
</PRE>
|
|
</TT> <P>
|
|
The execution of the above <A REL=DEFINITION HREF="m_loop.htm#loop"><B>loop</B></A> is equivalent to the execution of the following code: <P>
|
|
|
|
<PRE>
|
|
(block nil
|
|
(let* ((a 1)
|
|
(b (+ a 2))
|
|
(c (+ b 3)))
|
|
(tagbody
|
|
(next-loop (return (list a b c))
|
|
(go next-loop)
|
|
end-loop))))
|
|
</PRE>
|
|
</TT> <P>
|
|
If the values of previously bound variables are not needed for the initialization of other local variables, an <TT>and</TT> clause can be used to specify that the bindings are to occur in <A REL=DEFINITION HREF="26_glo_p.htm#parallel"><I>parallel</I></A>: <P>
|
|
<PRE>
|
|
(loop with a = 1
|
|
and b = 2
|
|
and c = 3
|
|
return (list a b c))
|
|
=> (1 2 3)
|
|
</PRE>
|
|
</TT> <P>
|
|
The execution of the above loop is equivalent to the execution of the following code: <P>
|
|
|
|
<PRE>
|
|
(block nil
|
|
(let ((a 1)
|
|
(b 2)
|
|
(c 3))
|
|
(tagbody
|
|
(next-loop (return (list a b c))
|
|
(go next-loop)
|
|
end-loop))))
|
|
</PRE>
|
|
</TT> <P>
|
|
|
|
|
|
<A REL=CHILD HREF="06_abba.htm"><H2>
|
|
6.1.2.2.1 Examples of WITH clause</H2></A><P><P><HR>The following <A REL=META HREF="../Front/X3J13Iss.htm">X3J13 cleanup issue</A>, <I>not part of the specification</I>, applies to this section:<P><UL><LI> <A REL=CHILD HREF="../Issues/iss223.htm">LOOP-MISCELLANEOUS-REPAIRS:FIX</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>
|