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

56 lines
6.9 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.1</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_ab.htm">
<LINK REL=UP HREF="06_ab.htm">
<LINK REL=NEXT HREF="06_abaa.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_ab.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_abaa.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
<HR>
<H2>
6.1.2.1 Iteration Control</H2> <P>
Iteration control clauses allow direction of <A REL=DEFINITION HREF="m_loop.htm#loop"><B>loop</B></A> iteration. The <A REL=DEFINITION HREF="26_glo_l.htm#loop_keyword"><I>loop keywords</I></A> <TT>for</TT> and <TT>as</TT> designate iteration control clauses. Iteration control clauses differ with respect to the specification of termination tests and to the initialization and <I>stepping</I>[1] of loop variables. Iteration clauses by themselves do not cause the Loop Facility to return values, but they can be used in conjunction with value-accumulation clauses to return values. <P>
All variables are initialized in the loop prologue. A <A REL=DEFINITION HREF="26_glo_v.htm#variable"><I>variable</I></A> <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>binding</I></A> has <A REL=DEFINITION HREF="26_glo_l.htm#lexical_scope"><I>lexical scope</I></A> unless it is proclaimed <A REL=DEFINITION HREF="d_specia.htm#special"><B>special</B></A>; thus, by default, the variable can be <I>accessed</I> only by <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>forms</I></A> that lie textually within the <A REL=DEFINITION HREF="m_loop.htm#loop"><B>loop</B></A>. Stepping assignments are made in the loop body before any other <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>forms</I></A> are evaluated in the body. <P>
The variable argument in iteration control clauses can be a destructuring list. A destructuring list is a <A REL=DEFINITION HREF="26_glo_t.htm#tree"><I>tree</I></A> whose <A REL=DEFINITION HREF="26_glo_n.htm#non-nil"><I>non-nil</I></A> <A REL=DEFINITION HREF="26_glo_a.htm#atom"><I>atoms</I></A> are <A REL=DEFINITION HREF="26_glo_v.htm#variable"><I>variable</I></A> <A REL=DEFINITION HREF="26_glo_n.htm#name"><I>names</I></A>. See <A REL=CHILD HREF="06_aag.htm">Section 6.1.1.7 (Destructuring)</A>. <P>
The iteration control clauses <TT>for</TT>, <TT>as</TT>, and <TT>repeat</TT> must precede any other loop clauses, except <TT>initially</TT>, <TT>with</TT>, and <TT>named</TT>, since they establish variable <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>bindings</I></A>. When iteration control clauses are used in a <A REL=DEFINITION HREF="m_loop.htm#loop"><B>loop</B></A>, the corresponding termination tests in the loop body are evaluated before any other loop body code is executed. <P>
If multiple iteration clauses are used to control iteration, variable initialization and <I>stepping</I>[1] occur <A REL=DEFINITION HREF="26_glo_s.htm#sequentially"><I>sequentially</I></A> by default. The <TT>and</TT> construct can be used to connect two or more iteration clauses when <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> and <I>stepping</I>[1] are not necessary. The iteration behavior of clauses joined by <TT>and</TT> is analogous to the behavior of the macro <A REL=DEFINITION HREF="m_do_do.htm#do"><B>do</B></A> with respect to <A REL=DEFINITION HREF="m_do_do.htm#doST"><B>do*</B></A>. <P>
The <TT>for</TT> and <TT>as</TT> clauses iterate by using one or more local loop variables that are initialized to some value and that can be modified or <I>stepped</I>[1] after each iteration. For these clauses, iteration terminates when a local variable reaches some supplied value or when some other loop clause terminates iteration. At each iteration, variables can be <I>stepped</I>[1] by an increment or a decrement or can be assigned a new value by the evaluation of a <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>). Destructuring can be used to assign values to variables during iteration. <P>
The <TT>for</TT> and <TT>as</TT> keywords are synonyms; they can be used interchangeably. There are seven syntactic formats for these constructs. In each syntactic format, the <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> of <I>var</I> can be supplied by the optional <I>type-spec</I> argument. If <I>var</I> is a destructuring list, the <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> supplied by the <I>type-spec</I> argument must appropriately match the elements of the list. By convention, <TT>for</TT> introduces new iterations and <TT>as</TT> introduces iterations that depend on a previous iteration specification. <P>
<A REL=CHILD HREF="06_abaa.htm"><H2>
6.1.2.1.1 The for-as-arithmetic subclause</H2></A><P>
<A REL=CHILD HREF="06_abab.htm"><H2>
6.1.2.1.2 The for-as-in-list subclause</H2></A><P>
<A REL=CHILD HREF="06_abac.htm"><H2>
6.1.2.1.3 The for-as-on-list subclause</H2></A><P>
<A REL=CHILD HREF="06_abad.htm"><H2>
6.1.2.1.4 The for-as-equals-then subclause</H2></A><P>
<A REL=CHILD HREF="06_abae.htm"><H2>
6.1.2.1.5 The for-as-across subclause</H2></A><P>
<A REL=CHILD HREF="06_abaf.htm"><H2>
6.1.2.1.6 The for-as-hash subclause</H2></A><P>
<A REL=CHILD HREF="06_abag.htm"><H2>
6.1.2.1.7 The for-as-package subclause</H2></A><P><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>