1
0
Fork 0
cl-sites/novaspec.org/cl/6_1_The_LOOP_Facility.html

6325 lines
142 KiB
HTML
Raw Normal View History

2025-02-05 18:52:26 +01:00
<!DOCTYPE HTML>
<HTML LANG="en-us"
><HEAD
><TITLE
>6.1 The LOOP Facility | Common Lisp Nova Spec</TITLE
><META CHARSET="US-ASCII"
><LINK REL="canonical" HREF="6_1_The_LOOP_Facility.html"
><LINK REL="next" HREF="6_2_Iteration_Dictionary.html" TYPE="text/html" TITLE="6.2 Iteration Dictionary"
><LINK REL="prev" HREF="6_Iteration.html" TYPE="text/html" TITLE="6. Iteration"
><LINK REL="up" HREF="6_Iteration.html" TYPE="text/html" TITLE="6. Iteration"
><LINK REL="start" HREF="index.html" TYPE="text/html" TITLE="Common Lisp Nova Spec"
><META NAME="VIEWPORT" CONTENT="width=device-width, initial-scale=1.0"
><LINK REL="STYLESHEET" HREF="dpans.css%3F3909942064.css"
><SCRIPT SRC="dpans.js%3F3909942064"
></SCRIPT
><SCRIPT SRC="apropos.js%3F3909942064"
></SCRIPT
></HEAD
><BODY
><DIV
><DIV CLASS="topnav"
><DIV CLASS="breadcrumb"
><SPAN CLASS="breadcrumb-item"
><A HREF="index.html"
>Common Lisp Nova Spec</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="6_Iteration.html"
>6. Iteration</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="6_1_The_LOOP_Facility.html"
>6.1 The LOOP Facility</A
></SPAN
></DIV
><DIV CLASS="apropos"
><DIV CLASS="apropos-io"
><A HREF="6_Iteration.html" CLASS="prev"
>&#8592;</A
><SPAN ID="apropos-label"
>Apropos </SPAN
><INPUT ID="apropos" AUTOFOCUS="AUTOFOCUS" PLACEHOLDER="Type here to search" ONINPUT="aproposInput(this);" ONKEYUP="aproposKeyup(event);" ONCHANGE="aproposChange(this);" ONFOCUS="aproposFocus(this);" ONFOCUSOUT="aproposFocusout(this);"
><A HREF="6_2_Iteration_Dictionary.html" CLASS="next"
>&#8594;</A
></DIV
><DIV ID="apropos-res"
></DIV
></DIV
></DIV
><DIV CLASS="matter"
><SECTION
><H2 ID="sec_6_1" CLASS="HeadII"
>6.1 The LOOP Facility</H2
><UL CLASS="subtoc"
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_1"
>6.1.1 Overview of the Loop Facility</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_1_1"
>6.1.1.1 Simple vs Extended Loop</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_1_1_1"
>6.1.1.1.1 Simple Loop</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_1_1_2"
>6.1.1.1.2 Extended Loop</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_1_2"
>6.1.1.2 Loop Keywords</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_1_3"
>6.1.1.3 Parsing Loop Clauses</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_1_4"
>6.1.1.4 Expanding Loop Forms</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_1_5"
>6.1.1.5 Summary of Loop Clauses</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_1_5_1"
>6.1.1.5.1 Summary of Variable Initialization and Stepping Clauses</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_1_5_2"
>6.1.1.5.2 Summary of Value Accumulation Clauses</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_1_5_3"
>6.1.1.5.3 Summary of Termination Test Clauses</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_1_5_4"
>6.1.1.5.4 Summary of Unconditional Execution Clauses</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_1_5_5"
>6.1.1.5.5 Summary of Conditional Execution Clauses</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_1_5_6"
>6.1.1.5.6 Summary of Miscellaneous Clauses</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_1_6"
>6.1.1.6 Order of Execution</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_1_7"
>6.1.1.7 Destructuring</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_1_8"
>6.1.1.8 Restrictions on Side-Effects</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_2"
>6.1.2 Variable Initialization and Stepping Clauses</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_2_1"
>6.1.2.1 Iteration Control</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_2_1_1"
>6.1.2.1.1 The for-as-arithmetic subclause</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_2_1_1_1"
>6.1.2.1.1.1 Examples of for-as-arithmetic subclause</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_2_1_2"
>6.1.2.1.2 The for-as-in-list subclause</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_2_1_2_1"
>6.1.2.1.2.1 Examples of for-as-in-list subclause</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_2_1_3"
>6.1.2.1.3 The for-as-on-list subclause</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_2_1_3_1"
>6.1.2.1.3.1 Examples of for-as-on-list subclause</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_2_1_4"
>6.1.2.1.4 The for-as-equals-then subclause</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_2_1_4_1"
>6.1.2.1.4.1 Examples of for-as-equals-then subclause</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_2_1_5"
>6.1.2.1.5 The for-as-across subclause</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_2_1_5_1"
>6.1.2.1.5.1 Examples of for-as-across subclause</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_2_1_6"
>6.1.2.1.6 The for-as-hash subclause</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_2_1_7"
>6.1.2.1.7 The for-as-package subclause</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_2_1_7_1"
>6.1.2.1.7.1 Examples of for-as-package subclause</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_2_2"
>6.1.2.2 Local Variable Initializations</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_2_2_1"
>6.1.2.2.1 Examples of WITH clause</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_3"
>6.1.3 Value Accumulation Clauses</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_3_1"
>6.1.3.1 Examples of COLLECT clause</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_3_2"
>6.1.3.2 Examples of APPEND and NCONC clauses</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_3_3"
>6.1.3.3 Examples of COUNT clause</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_3_4"
>6.1.3.4 Examples of MAXIMIZE and MINIMIZE clauses</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_3_5"
>6.1.3.5 Examples of SUM clause</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_4"
>6.1.4 Termination Test Clauses</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_4_1"
>6.1.4.1 Examples of REPEAT clause</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_4_2"
>6.1.4.2 Examples of ALWAYS, NEVER, and THEREIS clauses</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_4_3"
>6.1.4.3 Examples of WHILE and UNTIL clauses</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_5"
>6.1.5 Unconditional Execution Clauses</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_5_1"
>6.1.5.1 Examples of unconditional execution</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_6"
>6.1.6 Conditional Execution Clauses</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_6_1"
>6.1.6.1 Examples of WHEN clause</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_7"
>6.1.7 Miscellaneous Clauses</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_7_1"
>6.1.7.1 Control Transfer Clauses</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_7_1_1"
>6.1.7.1.1 Examples of NAMED clause</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_7_2"
>6.1.7.2 Initial and Final Execution</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_8"
>6.1.8 Examples of Miscellaneous Loop Features</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_8_1"
>6.1.8.1 Examples of clause grouping</A
></LI
><LI
><A HREF="6_1_The_LOOP_Facility.html#sec_6_1_9"
>6.1.9 Notes about Loop</A
></LI
></UL
><H3 ID="sec_6_1_1" CLASS="HeadIII"
>6.1.1 Overview of the Loop Facility</H3
><P CLASS="j"
>The <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> <A HREF="26_1_Glossary.html#macro"
><EM CLASS="term"
>macro</EM
></A
> performs iteration.</P
><H4 ID="sec_6_1_1_1" CLASS="HeadIV"
>6.1.1.1 Simple vs Extended Loop</H4
><P CLASS="j"
><A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>forms</EM
></A
> are partitioned into two categories: simple <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>forms</EM
></A
> and extended <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>forms</EM
></A
>.</P
><H5 ID="sec_6_1_1_1_1" CLASS="HeadV"
>6.1.1.1.1 Simple Loop</H5
><P CLASS="j"
>A simple <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> is one that has a body containing only <A HREF="26_1_Glossary.html#compound_form"
><EM CLASS="term"
>compound forms</EM
></A
>. Each <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> is <A HREF="26_1_Glossary.html#evaluate"
><EM CLASS="term"
>evaluated</EM
></A
> in turn from left to right. When the last <VAR CLASS="param"
>form</VAR
> has been <A HREF="26_1_Glossary.html#evaluate"
><EM CLASS="term"
>evaluated</EM
></A
>, then the first <VAR CLASS="param"
>form</VAR
> is evaluated again, and so on, in a never-ending cycle. A simple <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> establishes an <A HREF="26_1_Glossary.html#implicit_block"
><EM CLASS="term"
>implicit block</EM
></A
> named <SPAN CLASS="misc"
><B
>nil</B
></SPAN
>. The execution of a simple <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> can be terminated by explicitly transfering control to the <A HREF="26_1_Glossary.html#implicit_block"
><EM CLASS="term"
>implicit block</EM
></A
> (using <A HREF="f_return.html" CLASS="macref"
><B
>return</B
></A
> or <A HREF="f_return-from.html" CLASS="specref"
><B
>return-from</B
></A
>) or to some <A HREF="26_1_Glossary.html#exit_point"
><EM CLASS="term"
>exit point</EM
></A
> outside of the <A HREF="26_1_Glossary.html#block"
><EM CLASS="term"
>block</EM
></A
> (<I CLASS="i"
><I
>e.g.</I
></I
>, using <A HREF="f_throw.html" CLASS="specref"
><B
>throw</B
></A
>, <A HREF="f_go.html" CLASS="specref"
><B
>go</B
></A
>, or <A HREF="f_return-from.html" CLASS="specref"
><B
>return-from</B
></A
>).</P
><H5 ID="sec_6_1_1_1_2" CLASS="HeadV"
>6.1.1.1.2 Extended Loop</H5
><P CLASS="j"
>An extended <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> is one that has a body containing <A HREF="26_1_Glossary.html#atomic"
><EM CLASS="term"
>atomic</EM
></A
> <A HREF="26_1_Glossary.html#expression"
><EM CLASS="term"
>expressions</EM
></A
>. When the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> <A HREF="26_1_Glossary.html#macro"
><EM CLASS="term"
>macro</EM
></A
> processes such a <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
>, it invokes a facility that is commonly called &#8220;the Loop Facility.&#8221; </P
><P CLASS="j"
>The Loop Facility provides standardized access to mechanisms commonly used in iterations through Loop schemas, which are introduced by <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keywords</EM
></A
>. </P
><P CLASS="j"
>The body of an extended <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> is divided into <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> clauses, each which is in turn made up of <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keywords</EM
></A
> and <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>forms</EM
></A
>.</P
><H4 ID="sec_6_1_1_2" CLASS="HeadIV"
>6.1.1.2 Loop Keywords</H4
><P CLASS="j"
><A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>Loop keywords</EM
></A
> are not true <A HREF="26_1_Glossary.html#keyword"
><EM CLASS="term"
>keywords</EM
></A
><SPAN CLASS="meaning"
><SUB CLASS="sub"
><SPAN CLASS="cmr"
>1</SPAN
></SUB
></SPAN
>; they are special <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbols</EM
></A
>, recognized by <A HREF="26_1_Glossary.html#name"
><EM CLASS="term"
>name</EM
></A
> rather than <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
> identity, that are meaningful only to the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> facility. A <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keyword</EM
></A
> is a <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbol</EM
></A
> but is recognized by its <A HREF="26_1_Glossary.html#name"
><EM CLASS="term"
>name</EM
></A
> (not its identity), regardless of the <A HREF="26_1_Glossary.html#package"
><EM CLASS="term"
>packages</EM
></A
> in which it is <A HREF="26_1_Glossary.html#accessible"
><EM CLASS="term"
>accessible</EM
></A
>. </P
><P CLASS="j"
>In general, <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keywords</EM
></A
> are not <A HREF="26_1_Glossary.html#external_symbol"
><EM CLASS="term"
>external symbols</EM
></A
> of the <SPAN CLASS="packref"
><SPAN CLASS="cmtt"
>COMMON-LISP</SPAN
></SPAN
> <A HREF="26_1_Glossary.html#package"
><EM CLASS="term"
>package</EM
></A
>, except in the coincidental situation that a <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbol</EM
></A
> with the same name as a <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keyword</EM
></A
> was needed for some other purpose in Common Lisp. For example, there is a <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbol</EM
></A
> in the <SPAN CLASS="packref"
><SPAN CLASS="cmtt"
>COMMON-LISP</SPAN
></SPAN
> <A HREF="26_1_Glossary.html#package"
><EM CLASS="term"
>package</EM
></A
> whose <A HREF="26_1_Glossary.html#name"
><EM CLASS="term"
>name</EM
></A
> is <CODE CLASS="f"
>"UNLESS"</CODE
> but not one whose <A HREF="26_1_Glossary.html#name"
><EM CLASS="term"
>name</EM
></A
> is <CODE CLASS="f"
>"UNTIL"</CODE
>. </P
><P CLASS="j"
>If no <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keywords</EM
></A
> are supplied in a <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
>, the Loop Facility executes the loop body repeatedly; see <A HREF="6_1_The_LOOP_Facility.html#sec_6_1_1_1_1" CLASS="secref"
><SPAN CLASS="cmr"
>Section</SPAN
> <SPAN CLASS="cmr"
>6.1.1.1.1</SPAN
> <SPAN CLASS="cmr"
>(Simple</SPAN
> <SPAN CLASS="cmr"
>Loop)</SPAN
></A
>.</P
><H4 ID="sec_6_1_1_3" CLASS="HeadIV"
>6.1.1.3 Parsing Loop Clauses</H4
><P CLASS="j"
>The syntactic parts of an extended <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> are called clauses; the rules for parsing are determined by that clause&#8217;s keyword. The following example shows a <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> with six clauses:</P
><PRE CLASS="screen"
>(loop for i from 1 to (compute-top-value) ; first clause
while (not (unacceptable i)) ; second clause
collect (square i) ; third clause
do (format t "Working on ~D now" i) ; fourth clause
when (evenp i) ; fifth clause
do (format t "~D is a non-odd number" i)
finally (format t "About to exit!")) ; sixth clause</PRE
><P CLASS="j"
>Each <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keyword</EM
></A
> introduces either a compound loop clause or a simple loop clause that can consist of a <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keyword</EM
></A
> followed by a single <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
>. The number of <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>forms</EM
></A
> in a clause is determined by the <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keyword</EM
></A
> that begins the clause and by the auxiliary keywords in the clause. The keywords <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>do</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>doing</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>initially</SPAN
></SPAN
>, and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>finally</SPAN
></SPAN
> are the only loop keywords that can take any number of <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>forms</EM
></A
> and group them as an <A HREF="26_1_Glossary.html#implicit_progn"
><EM CLASS="term"
>implicit progn</EM
></A
>. </P
><P CLASS="j"
>Loop clauses can contain auxiliary keywords, which are sometimes called prepositions. For example, the first clause in the code above includes the prepositions <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>from</SPAN
></SPAN
> and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>to</SPAN
></SPAN
>, which mark the value from which stepping begins and the value at which stepping ends. </P
><P CLASS="j"
>For detailed information about <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> syntax, see the <A HREF="26_1_Glossary.html#macro"
><EM CLASS="term"
>macro</EM
></A
> <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
>.</P
><H4 ID="sec_6_1_1_4" CLASS="HeadIV"
>6.1.1.4 Expanding Loop Forms</H4
><P CLASS="j"
>A <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> <A HREF="26_1_Glossary.html#macro_form"
><EM CLASS="term"
>macro form</EM
></A
> expands into a <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> containing one or more binding forms (that <A HREF="26_1_Glossary.html#establish"
><EM CLASS="term"
>establish</EM
></A
> <A HREF="26_1_Glossary.html#binding"
><EM CLASS="term"
>bindings</EM
></A
> of loop variables) and a <A HREF="f_block.html" CLASS="specref"
><B
>block</B
></A
> and a <A HREF="f_tagbody.html" CLASS="specref"
><B
>tagbody</B
></A
> (that express a looping control structure). The variables established in <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> are bound as if by <A HREF="f_let.html" CLASS="specref"
><B
>let</B
></A
> or <SPAN CLASS="misc"
><B
>lambda</B
></SPAN
>. </P
><P CLASS="j"
>Implementations can interleave the setting of initial values with the <A HREF="26_1_Glossary.html#binding"
><EM CLASS="term"
>bindings</EM
></A
>. However, the assignment of the initial values is always calculated in the order specified by the user. A variable is thus sometimes bound to a meaningless value of the correct <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
>, and then later in the prologue it is set to the true initial value by using <A HREF="f_setq.html" CLASS="specref"
><B
>setq</B
></A
>. One implication of this interleaving is that it is <A HREF="26_1_Glossary.html#implementation-dependent"
><EM CLASS="term"
>implementation-dependent</EM
></A
> whether the <A HREF="26_1_Glossary.html#lexical_environment"
><EM CLASS="term"
>lexical environment</EM
></A
> in which the initial value <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>forms</EM
></A
> (variously called the <VAR CLASS="param"
>form1</VAR
>, <VAR CLASS="param"
>form2</VAR
>, <VAR CLASS="param"
>form3</VAR
>, <VAR CLASS="param"
>step-fun</VAR
>, <VAR CLASS="param"
>vector</VAR
>, <VAR CLASS="param"
>hash-table</VAR
>, and <VAR CLASS="param"
>package</VAR
>) in any <VAR CLASS="param"
>for-as-subclause</VAR
>, except <VAR CLASS="param"
>for-as-equals-then</VAR
>, are <A HREF="26_1_Glossary.html#evaluate"
><EM CLASS="term"
>evaluated</EM
></A
> includes only the loop variables preceding that <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> or includes more or all of the loop variables; the <VAR CLASS="param"
>form1</VAR
> and <VAR CLASS="param"
>form2</VAR
> in a <VAR CLASS="param"
>for-as-equals-then</VAR
> form includes the <A HREF="26_1_Glossary.html#lexical_environment"
><EM CLASS="term"
>lexical environment</EM
></A
> of all the loop variables. </P
><P CLASS="j"
>After the <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> is expanded, it consists of three basic parts in the <A HREF="f_tagbody.html" CLASS="specref"
><B
>tagbody</B
></A
>: the loop prologue, the loop body, and the loop epilogue.</P
><DL
><DT
><B CLASS="b"
>Loop prologue</B
></DT
><DD
><P CLASS="j"
>The loop prologue contains <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>forms</EM
></A
> that are executed before iteration begins, such as any automatic variable initializations prescribed by the <VAR CLASS="param"
>variable</VAR
> clauses, along with any <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>initially</SPAN
></SPAN
> clauses in the order they appear in the source.</P
></DD
><DT
><B CLASS="b"
>Loop body</B
></DT
><DD
><P CLASS="j"
>The loop body contains those <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>forms</EM
></A
> that are executed during iteration, including application-specific calculations, termination tests, and variable <EM CLASS="term"
>stepping</EM
><SPAN CLASS="meaning"
><SUB CLASS="sub"
><SPAN CLASS="cmr"
>1</SPAN
></SUB
></SPAN
>.</P
></DD
><DT
><B CLASS="b"
>Loop epilogue</B
></DT
><DD
><P CLASS="j"
>The loop epilogue contains <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>forms</EM
></A
> that are executed after iteration terminates, such as <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>finally</SPAN
></SPAN
> clauses, if any, along with any implicit return value from an <VAR CLASS="param"
>accumulation</VAR
> clause or an <VAR CLASS="param"
>termination-test</VAR
> clause.</P
></DD
></DL
><P CLASS="j"
>Some clauses from the source <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> contribute code only to the loop prologue; these clauses must come before other clauses that are in the main body of the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> form. Others contribute code only to the loop epilogue. All other clauses contribute to the final translated <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> in the same order given in the original source <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> of the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
>. </P
><P CLASS="j"
>Expansion of the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> macro produces an <A HREF="26_1_Glossary.html#implicit_block"
><EM CLASS="term"
>implicit block</EM
></A
> named <SPAN CLASS="misc"
><B
>nil</B
></SPAN
> unless <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>named</SPAN
></SPAN
> is supplied. Thus, <A HREF="f_return-from.html" CLASS="specref"
><B
>return-from</B
></A
> (and sometimes <A HREF="f_return.html" CLASS="macref"
><B
>return</B
></A
>) can be used to return values from <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> or to exit <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
>.</P
><H4 ID="sec_6_1_1_5" CLASS="HeadIV"
>6.1.1.5 Summary of Loop Clauses</H4
><P CLASS="j"
>Loop clauses fall into one of the following categories:</P
><H5 ID="sec_6_1_1_5_1" CLASS="HeadV"
>6.1.1.5.1 Summary of Variable Initialization and Stepping Clauses</H5
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>for</SPAN
></SPAN
> and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>as</SPAN
></SPAN
> constructs provide iteration control clauses that establish a variable to be initialized. <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>for</SPAN
></SPAN
> and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>as</SPAN
></SPAN
> clauses can be combined with the loop keyword <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>and</SPAN
></SPAN
> to get <A HREF="26_1_Glossary.html#parallel"
><EM CLASS="term"
>parallel</EM
></A
> initialization and <EM CLASS="term"
>stepping</EM
><SPAN CLASS="meaning"
><SUB CLASS="sub"
><SPAN CLASS="cmr"
>1</SPAN
></SUB
></SPAN
>. Otherwise, the initialization and <EM CLASS="term"
>stepping</EM
><SPAN CLASS="meaning"
><SUB CLASS="sub"
><SPAN CLASS="cmr"
>1</SPAN
></SUB
></SPAN
> are <A HREF="26_1_Glossary.html#sequential"
><EM CLASS="term"
>sequential</EM
></A
>. </P
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>with</SPAN
></SPAN
> construct is similar to a single <A HREF="f_let.html" CLASS="specref"
><B
>let</B
></A
> clause. <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>with</SPAN
></SPAN
> clauses can be combined using the <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keyword</EM
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>and</SPAN
></SPAN
> to get <A HREF="26_1_Glossary.html#parallel"
><EM CLASS="term"
>parallel</EM
></A
> initialization. </P
><P CLASS="j"
>For more information, see <A HREF="6_1_The_LOOP_Facility.html#sec_6_1_2" CLASS="secref"
><SPAN CLASS="cmr"
>Section</SPAN
> <SPAN CLASS="cmr"
>6.1.2</SPAN
> <SPAN CLASS="cmr"
>(Variable</SPAN
> <SPAN CLASS="cmr"
>Initialization</SPAN
> <SPAN CLASS="cmr"
>and</SPAN
> <SPAN CLASS="cmr"
>Stepping</SPAN
> <SPAN CLASS="cmr"
>Clauses)</SPAN
></A
>.</P
><H5 ID="sec_6_1_1_5_2" CLASS="HeadV"
>6.1.1.5.2 Summary of Value Accumulation Clauses</H5
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>collect</SPAN
></SPAN
> (or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>collecting</SPAN
></SPAN
>) construct takes one <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> in its clause and adds the value of that <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> to the end of a <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
> of values. By default, the <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
> of values is returned when the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> finishes. </P
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>append</SPAN
></SPAN
> (or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>appending</SPAN
></SPAN
>) construct takes one <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> in its clause and appends the value of that <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> to the end of a <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
> of values. By default, the <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
> of values is returned when the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> finishes. </P
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>nconc</SPAN
></SPAN
> (or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>nconcing</SPAN
></SPAN
>) construct is similar to the <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>append</SPAN
></SPAN
> construct, but its <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
> values are concatenated as if by the function <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>nconc</SPAN
></SPAN
>. By default, the <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
> of values is returned when the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> finishes. </P
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>sum</SPAN
></SPAN
> (or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>summing</SPAN
></SPAN
>) construct takes one <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> in its clause that must evaluate to a <A HREF="26_1_Glossary.html#number"
><EM CLASS="term"
>number</EM
></A
> and accumulates the sum of all these <A HREF="26_1_Glossary.html#number"
><EM CLASS="term"
>numbers</EM
></A
>. By default, the cumulative sum is returned when the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> finishes. </P
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>count</SPAN
></SPAN
> (or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>counting</SPAN
></SPAN
>) construct takes one <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> in its clause and counts the number of times that the <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> evaluates to <A HREF="26_1_Glossary.html#true"
><EM CLASS="term"
>true</EM
></A
>. By default, the count is returned when the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> finishes. </P
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>minimize</SPAN
></SPAN
> (or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>minimizing</SPAN
></SPAN
>) construct takes one <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> in its clause and determines the minimum value obtained by evaluating that <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
>. By default, the minimum value is returned when the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> finishes. </P
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>maximize</SPAN
></SPAN
> (or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>maximizing</SPAN
></SPAN
>) construct takes one <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> in its clause and determines the maximum value obtained by evaluating that <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
>. By default, the maximum value is returned when the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> finishes. </P
><P CLASS="j"
>For more information, see <A HREF="6_1_The_LOOP_Facility.html#sec_6_1_3" CLASS="secref"
><SPAN CLASS="cmr"
>Section</SPAN
> <SPAN CLASS="cmr"
>6.1.3</SPAN
> <SPAN CLASS="cmr"
>(Value</SPAN
> <SPAN CLASS="cmr"
>Accumulation</SPAN
> <SPAN CLASS="cmr"
>Clauses)</SPAN
></A
>.</P
><H5 ID="sec_6_1_1_5_3" CLASS="HeadV"
>6.1.1.5.3 Summary of Termination Test Clauses</H5
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>for</SPAN
></SPAN
> and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>as</SPAN
></SPAN
> constructs provide a termination test that is determined by the iteration control clause. </P
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>repeat</SPAN
></SPAN
> construct causes termination after a specified number of iterations. (It uses an internal variable to keep track of the number of iterations.) </P
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>while</SPAN
></SPAN
> construct takes one <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
>, a <VAR CLASS="param"
>test</VAR
>, and terminates the iteration if the <VAR CLASS="param"
>test</VAR
> evaluates to <A HREF="26_1_Glossary.html#false"
><EM CLASS="term"
>false</EM
></A
>. A <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>while</SPAN
></SPAN
> clause is equivalent to the expression <CODE CLASS="f"
>(if (not <VAR CLASS="param"
>test</VAR
>) (loop-finish))</CODE
>. </P
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>until</SPAN
></SPAN
> construct is the inverse of <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>while</SPAN
></SPAN
>; it terminates the iteration if the <VAR CLASS="param"
>test</VAR
> evaluates to any <A HREF="26_1_Glossary.html#non-nil"
><EM CLASS="term"
>non-nil</EM
></A
> value. An <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>until</SPAN
></SPAN
> clause is equivalent to the expression <CODE CLASS="f"
>(if <VAR CLASS="param"
>test</VAR
> (loop-finish))</CODE
>. </P
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>always</SPAN
></SPAN
> construct takes one <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> and terminates the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> if the <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> ever evaluates to <A HREF="26_1_Glossary.html#false"
><EM CLASS="term"
>false</EM
></A
>; in this case, the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> returns <SPAN CLASS="misc"
><B
>nil</B
></SPAN
>. Otherwise, it provides a default return value of <SPAN CLASS="misc"
><B
>t</B
></SPAN
>. </P
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>never</SPAN
></SPAN
> construct takes one <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> and terminates the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> if the <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> ever evaluates to <A HREF="26_1_Glossary.html#true"
><EM CLASS="term"
>true</EM
></A
>; in this case, the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> returns <SPAN CLASS="misc"
><B
>nil</B
></SPAN
>. Otherwise, it provides a default return value of <SPAN CLASS="misc"
><B
>t</B
></SPAN
>. </P
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>thereis</SPAN
></SPAN
> construct takes one <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> and terminates the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> if the <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> ever evaluates to a <A HREF="26_1_Glossary.html#non-nil"
><EM CLASS="term"
>non-nil</EM
></A
> <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
>; in this case, the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> returns that <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
>. Otherwise, it provides a default return value of <SPAN CLASS="misc"
><B
>nil</B
></SPAN
>. </P
><P CLASS="j"
>If multiple termination test clauses are specified, the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> terminates if any are satisfied. </P
><P CLASS="j"
>For more information, see <A HREF="6_1_The_LOOP_Facility.html#sec_6_1_4" CLASS="secref"
><SPAN CLASS="cmr"
>Section</SPAN
> <SPAN CLASS="cmr"
>6.1.4</SPAN
> <SPAN CLASS="cmr"
>(Termination</SPAN
> <SPAN CLASS="cmr"
>Test</SPAN
> <SPAN CLASS="cmr"
>Clauses)</SPAN
></A
>.</P
><H5 ID="sec_6_1_1_5_4" CLASS="HeadV"
>6.1.1.5.4 Summary of Unconditional Execution Clauses</H5
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>do</SPAN
></SPAN
> (or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>doing</SPAN
></SPAN
>) construct evaluates all <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>forms</EM
></A
> in its clause. </P
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>return</SPAN
></SPAN
> construct takes one <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
>. Any <A HREF="26_1_Glossary.html#value"
><EM CLASS="term"
>values</EM
></A
> returned by the <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> are immediately returned by the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> form. It is equivalent to the clause <CODE CLASS="f"
>do (return-from <I CLASS="i"
><I
>block-name</I
></I
> <I CLASS="i"
><I
>value</I
></I
>)</CODE
>, where <I CLASS="i"
><I
>block-name</I
></I
> is the name specified in a <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>named</SPAN
></SPAN
> clause, or <SPAN CLASS="misc"
><B
>nil</B
></SPAN
> if there is no <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>named</SPAN
></SPAN
> clause. </P
><P CLASS="j"
>For more information, see <A HREF="6_1_The_LOOP_Facility.html#sec_6_1_5" CLASS="secref"
><SPAN CLASS="cmr"
>Section</SPAN
> <SPAN CLASS="cmr"
>6.1.5</SPAN
> <SPAN CLASS="cmr"
>(Unconditional</SPAN
> <SPAN CLASS="cmr"
>Execution</SPAN
> <SPAN CLASS="cmr"
>Clauses)</SPAN
></A
>.</P
><H5 ID="sec_6_1_1_5_5" CLASS="HeadV"
>6.1.1.5.5 Summary of Conditional Execution Clauses</H5
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>if</SPAN
></SPAN
> and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>when</SPAN
></SPAN
> constructs take one <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> as a test and a clause that is executed when the test <A HREF="26_1_Glossary.html#yield"
><EM CLASS="term"
>yields</EM
></A
> <A HREF="26_1_Glossary.html#true"
><EM CLASS="term"
>true</EM
></A
>. The clause can be a value accumulation, unconditional, or another conditional clause; it can also be any combination of such clauses connected by the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>and</SPAN
></SPAN
> keyword. </P
><P CLASS="j"
>The <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>unless</SPAN
></SPAN
> construct is similar to the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>when</SPAN
></SPAN
> construct except that it complements the test result. </P
><P CLASS="j"
>The <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>else</SPAN
></SPAN
> construct provides an optional component of <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>if</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>when</SPAN
></SPAN
>, and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>unless</SPAN
></SPAN
> clauses that is executed when an <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>if</SPAN
></SPAN
> or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>when</SPAN
></SPAN
> test <A HREF="26_1_Glossary.html#yield"
><EM CLASS="term"
>yields</EM
></A
> <A HREF="26_1_Glossary.html#false"
><EM CLASS="term"
>false</EM
></A
> or when an <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>unless</SPAN
></SPAN
> test <A HREF="26_1_Glossary.html#yield"
><EM CLASS="term"
>yields</EM
></A
> <A HREF="26_1_Glossary.html#true"
><EM CLASS="term"
>true</EM
></A
>. The component is one of the clauses described under <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>if</SPAN
></SPAN
>. </P
><P CLASS="j"
>The <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>end</SPAN
></SPAN
> construct provides an optional component to mark the end of a conditional clause. </P
><P CLASS="j"
>For more information, see <A HREF="6_1_The_LOOP_Facility.html#sec_6_1_6" CLASS="secref"
><SPAN CLASS="cmr"
>Section</SPAN
> <SPAN CLASS="cmr"
>6.1.6</SPAN
> <SPAN CLASS="cmr"
>(Conditional</SPAN
> <SPAN CLASS="cmr"
>Execution</SPAN
> <SPAN CLASS="cmr"
>Clauses)</SPAN
></A
>.</P
><H5 ID="sec_6_1_1_5_6" CLASS="HeadV"
>6.1.1.5.6 Summary of Miscellaneous Clauses</H5
><P CLASS="j"
>The <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>named</SPAN
></SPAN
> construct gives a name for the <A HREF="26_1_Glossary.html#block"
><EM CLASS="term"
>block</EM
></A
> of the loop. </P
><P CLASS="j"
>The <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>initially</SPAN
></SPAN
> construct causes its <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>forms</EM
></A
> to be evaluated in the loop prologue, which precedes all <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> code except for initial settings supplied by the constructs <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>with</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>for</SPAN
></SPAN
>, or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>as</SPAN
></SPAN
>. </P
><P CLASS="j"
>The <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>finally</SPAN
></SPAN
> construct causes its <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>forms</EM
></A
> to be evaluated in the loop epilogue after normal iteration terminates. </P
><P CLASS="j"
>For more information, see <A HREF="6_1_The_LOOP_Facility.html#sec_6_1_7" CLASS="secref"
><SPAN CLASS="cmr"
>Section</SPAN
> <SPAN CLASS="cmr"
>6.1.7</SPAN
> <SPAN CLASS="cmr"
>(Miscellaneous</SPAN
> <SPAN CLASS="cmr"
>Clauses)</SPAN
></A
>.</P
><H4 ID="sec_6_1_1_6" CLASS="HeadIV"
>6.1.1.6 Order of Execution</H4
><P CLASS="j"
><SPAN CLASS="idx" DATA-KIND="idxtext" DATA-TERM="order of evaluation"
></SPAN
><SPAN CLASS="idx" DATA-KIND="idxtext" DATA-TERM="evaluation order"
></SPAN
>With the exceptions listed below, clauses are executed in the loop body in the order in which they appear in the source. Execution is repeated until a clause terminates the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> or until a <A HREF="f_return.html" CLASS="macref"
><B
>return</B
></A
>, <A HREF="f_go.html" CLASS="specref"
><B
>go</B
></A
>, or <A HREF="f_throw.html" CLASS="specref"
><B
>throw</B
></A
> form is encountered which transfers control to a point outside of the loop. The following actions are exceptions to the linear order of execution:</P
><UL
><LI
><P CLASS="j"
>All variables are initialized first, regardless of where the establishing clauses appear in the source. The order of initialization follows the order of these clauses.</P
></LI
><LI
><P CLASS="j"
>The code for any <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>initially</SPAN
></SPAN
> clauses is collected into one <A HREF="f_progn.html" CLASS="specref"
><B
>progn</B
></A
> in the order in which the clauses appear in the source. The collected code is executed once in the loop prologue after any implicit variable initializations.</P
></LI
><LI
><P CLASS="j"
>The code for any <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>finally</SPAN
></SPAN
> clauses is collected into one <A HREF="f_progn.html" CLASS="specref"
><B
>progn</B
></A
> in the order in which the clauses appear in the source. The collected code is executed once in the loop epilogue before any implicit values from the accumulation clauses are returned. Explicit returns anywhere in the source, however, will exit the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> without executing the epilogue code.</P
></LI
><LI
><P CLASS="j"
>A <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>with</SPAN
></SPAN
> clause introduces a variable <A HREF="26_1_Glossary.html#binding"
><EM CLASS="term"
>binding</EM
></A
> and an optional initial value. The initial values are calculated in the order in which the <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>with</SPAN
></SPAN
> clauses occur.</P
></LI
><LI
><P CLASS="j"
>Iteration control clauses implicitly perform the following actions:</P
><UL
><LI
><P CLASS="j"
>initialize variables;</P
></LI
><LI
><P CLASS="j"
><A HREF="26_1_Glossary.html#step"
><EM CLASS="term"
>step</EM
></A
> variables, generally between each execution of the loop body;</P
></LI
><LI
><P CLASS="j"
>perform termination tests, generally just before the execution of the loop body.</P
></LI
></UL
></LI
></UL
><H4 ID="sec_6_1_1_7" CLASS="HeadIV"
>6.1.1.7 Destructuring</H4
><P CLASS="j"
>The <VAR CLASS="param"
>d-type-spec</VAR
> argument is used for destructuring. If the <VAR CLASS="param"
>d-type-spec</VAR
> argument consists solely of the <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> <A HREF="t_fixnum.html" CLASS="typeref"
><B
>fixnum</B
></A
>, <A HREF="t_float.html" CLASS="typeref"
><B
>float</B
></A
>, <A HREF="t_t.html" CLASS="typeref"
><B
>t</B
></A
>, or <SPAN CLASS="misc"
><B
>nil</B
></SPAN
>, the <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>of-type</SPAN
></SPAN
> keyword is optional. The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>of-type</SPAN
></SPAN
> construct is optional in these cases to provide backwards compatibility; thus, the following two expressions are the same:</P
><PRE CLASS="screen"
>;;; This expression uses the old syntax for type specifiers.
(loop for i fixnum upfrom 3 ...)
;;; This expression uses the new syntax for type specifiers.
(loop for i of-type fixnum upfrom 3 ...)
;; Declare X and Y to be of type VECTOR and FIXNUM respectively.
(loop for (x y) of-type (vector fixnum)
in l do ...)</PRE
><P CLASS="j"
>A <A HREF="26_1_Glossary.html#type_specifier"
><EM CLASS="term"
>type specifier</EM
></A
> for a destructuring pattern is a <A HREF="26_1_Glossary.html#tree"
><EM CLASS="term"
>tree</EM
></A
> of <A HREF="26_1_Glossary.html#type_specifier"
><EM CLASS="term"
>type specifiers</EM
></A
> with the same shape as the <A HREF="26_1_Glossary.html#tree"
><EM CLASS="term"
>tree</EM
></A
> of <A HREF="26_1_Glossary.html#variable"
><EM CLASS="term"
>variable</EM
></A
> <A HREF="26_1_Glossary.html#name"
><EM CLASS="term"
>names</EM
></A
>, with the following exceptions:</P
><UL
><LI
><P CLASS="j"
>When aligning the <A HREF="26_1_Glossary.html#tree"
><EM CLASS="term"
>trees</EM
></A
>, an <A HREF="26_1_Glossary.html#atom"
><EM CLASS="term"
>atom</EM
></A
> in the <A HREF="26_1_Glossary.html#tree"
><EM CLASS="term"
>tree</EM
></A
> of <A HREF="26_1_Glossary.html#type_specifier"
><EM CLASS="term"
>type specifiers</EM
></A
> that matches a <A HREF="26_1_Glossary.html#cons"
><EM CLASS="term"
>cons</EM
></A
> in the variable tree declares the same <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> for each variable in the subtree rooted at the <A HREF="26_1_Glossary.html#cons"
><EM CLASS="term"
>cons</EM
></A
>.</P
></LI
><LI
><P CLASS="j"
>A <A HREF="26_1_Glossary.html#cons"
><EM CLASS="term"
>cons</EM
></A
> in the <A HREF="26_1_Glossary.html#tree"
><EM CLASS="term"
>tree</EM
></A
> of <A HREF="26_1_Glossary.html#type_specifier"
><EM CLASS="term"
>type specifiers</EM
></A
> that matches an <A HREF="26_1_Glossary.html#atom"
><EM CLASS="term"
>atom</EM
></A
> in the <A HREF="26_1_Glossary.html#tree"
><EM CLASS="term"
>tree</EM
></A
> of <A HREF="26_1_Glossary.html#variable"
><EM CLASS="term"
>variable</EM
></A
> <A HREF="26_1_Glossary.html#name"
><EM CLASS="term"
>names</EM
></A
> is a <A HREF="26_1_Glossary.html#compound_type_specifier"
><EM CLASS="term"
>compound type specifier</EM
></A
>.</P
></LI
></UL
><P CLASS="j"
>Destructuring allows <A HREF="26_1_Glossary.html#binding"
><EM CLASS="term"
>binding</EM
></A
> of a set of variables to a corresponding set of values anywhere that a value can normally be bound to a single variable. During <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> expansion, each variable in the variable list is matched with the values in the values list. If there are more variables in the variable list than there are values in the values list, the remaining variables are given a value of <SPAN CLASS="misc"
><B
>nil</B
></SPAN
>. If there are more values than variables listed, the extra values are discarded. </P
><P CLASS="j"
>To assign values from a list to the variables <CODE CLASS="f"
>a</CODE
>, <CODE CLASS="f"
>b</CODE
>, and <CODE CLASS="f"
>c</CODE
>, the <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>for</SPAN
></SPAN
> clause could be used to bind the variable <CODE CLASS="f"
>numlist</CODE
> to the <A HREF="26_1_Glossary.html#car"
><EM CLASS="term"
>car</EM
></A
> of the supplied <VAR CLASS="param"
>form</VAR
>, and then another <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>for</SPAN
></SPAN
> clause could be used to bind the variables <CODE CLASS="f"
>a</CODE
>, <CODE CLASS="f"
>b</CODE
>, and <CODE CLASS="f"
>c</CODE
> <A HREF="26_1_Glossary.html#sequentially"
><EM CLASS="term"
>sequentially</EM
></A
>.</P
><PRE CLASS="screen"
>;; Collect values by using FOR constructs.
(loop for numlist in '((1 2 4.0) (5 6 8.3) (8 9 10.4))
for a of-type integer = (first numlist)
and b of-type integer = (second numlist)
and c of-type float = (third numlist)
collect (list c b a))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> ((4.0 2 1) (8.3 6 5) (10.4 9 8))</PRE
><P CLASS="j"
>Destructuring makes this process easier by allowing the variables to be bound in each loop iteration. <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>Types</EM
></A
> can be declared by using a list of <VAR CLASS="param"
>type-spec</VAR
> arguments. If all the <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>types</EM
></A
> are the same, a shorthand destructuring syntax can be used, as the second example illustrates.</P
><PRE CLASS="screen"
>;; Destructuring simplifies the process.
(loop for (a b c) of-type (integer integer float) in
'((1 2 4.0) (5 6 8.3) (8 9 10.4))
collect (list c b a))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> ((4.0 2 1) (8.3 6 5) (10.4 9 8))
;; If all the types are the same, this way is even simpler.
(loop for (a b c) of-type float in
'((1.0 2.0 4.0) (5.0 6.0 8.3) (8.0 9.0 10.4))
collect (list c b a))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> ((4.0 2.0 1.0) (8.3 6.0 5.0) (10.4 9.0 8.0))</PRE
><P CLASS="j"
>If destructuring is used to declare or initialize a number of groups of variables into <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>types</EM
></A
>, the <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keyword</EM
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>and</SPAN
></SPAN
> can be used to simplify the process further.</P
><PRE CLASS="screen"
>;; Initialize and declare variables in parallel by using the AND construct.
(loop with (a b) of-type float = '(1.0 2.0)
and (c d) of-type integer = '(3 4)
and (e f)
return (list a b c d e f))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (1.0 2.0 3 4 NIL NIL)</PRE
><P CLASS="j"
>If <SPAN CLASS="misc"
><B
>nil</B
></SPAN
> is used in a destructuring list, no variable is provided for its place.</P
><PRE CLASS="screen"
> (loop for (a nil b) = '(1 2 3)
do (return (list a b)))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (1 3)</PRE
><P CLASS="j"
>Note that <A HREF="26_1_Glossary.html#dotted_list"
><EM CLASS="term"
>dotted lists</EM
></A
> can specify destructuring.</P
><PRE CLASS="screen"
> (loop for (x . y) = '(1 . 2)
do (return y))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 2
(loop for ((a . b) (c . d)) of-type ((float . float) (integer . integer)) in
'(((1.2 . 2.4) (3 . 4)) ((3.4 . 4.6) (5 . 6)))
collect (list a b c d))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> ((1.2 2.4 3 4) (3.4 4.6 5 6))</PRE
><P CLASS="j"
>An error of <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> <A HREF="t_program-error.html" CLASS="typeref"
><B
>program-error</B
></A
> is signaled (at macro expansion time) if the same variable is bound twice in any variable-binding clause of a single <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> expression. Such variables include local variables, iteration control variables, and variables found by destructuring.</P
><H4 ID="sec_6_1_1_8" CLASS="HeadIV"
>6.1.1.8 Restrictions on Side-Effects</H4
><P CLASS="j"
>See <A HREF="3_6_Traversal_Rules_and_Side_Effects.html#sec_3_6" CLASS="secref"
><SPAN CLASS="cmr"
>Section</SPAN
> <SPAN CLASS="cmr"
>3.6</SPAN
> <SPAN CLASS="cmr"
>(Traversal</SPAN
> <SPAN CLASS="cmr"
>Rules</SPAN
> <SPAN CLASS="cmr"
>and</SPAN
> <SPAN CLASS="cmr"
>Side</SPAN
> <SPAN CLASS="cmr"
>Effects)</SPAN
></A
>.</P
><H3 ID="sec_6_1_2" CLASS="HeadIII"
>6.1.2 Variable Initialization and Stepping Clauses</H3
><H4 ID="sec_6_1_2_1" CLASS="HeadIV"
>6.1.2.1 Iteration Control</H4
><P CLASS="j"
>Iteration control clauses allow direction of <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> iteration. The <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keywords</EM
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>for</SPAN
></SPAN
> and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>as</SPAN
></SPAN
> designate iteration control clauses. Iteration control clauses differ with respect to the specification of termination tests and to the initialization and <EM CLASS="term"
>stepping</EM
><SPAN CLASS="meaning"
><SUB CLASS="sub"
><SPAN CLASS="cmr"
>1</SPAN
></SUB
></SPAN
> 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
><P CLASS="j"
>All variables are initialized in the loop prologue. A <A HREF="26_1_Glossary.html#variable"
><EM CLASS="term"
>variable</EM
></A
> <A HREF="26_1_Glossary.html#binding"
><EM CLASS="term"
>binding</EM
></A
> has <A HREF="26_1_Glossary.html#lexical_scope"
><EM CLASS="term"
>lexical scope</EM
></A
> unless it is proclaimed <A HREF="m_special.html" CLASS="declref"
><B
>special</B
></A
>; thus, by default, the variable can be <A HREF="26_1_Glossary.html#access"
><EM CLASS="term"
>accessed</EM
></A
> only by <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>forms</EM
></A
> that lie textually within the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
>. Stepping assignments are made in the loop body before any other <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>forms</EM
></A
> are evaluated in the body. </P
><P CLASS="j"
>The variable argument in iteration control clauses can be a destructuring list. A destructuring list is a <A HREF="26_1_Glossary.html#tree"
><EM CLASS="term"
>tree</EM
></A
> whose <A HREF="26_1_Glossary.html#non-nil"
><EM CLASS="term"
>non-nil</EM
></A
> <A HREF="26_1_Glossary.html#atom"
><EM CLASS="term"
>atoms</EM
></A
> are <A HREF="26_1_Glossary.html#variable"
><EM CLASS="term"
>variable</EM
></A
> <A HREF="26_1_Glossary.html#name"
><EM CLASS="term"
>names</EM
></A
>. See <A HREF="6_1_The_LOOP_Facility.html#sec_6_1_1_7" CLASS="secref"
><SPAN CLASS="cmr"
>Section</SPAN
> <SPAN CLASS="cmr"
>6.1.1.7</SPAN
> <SPAN CLASS="cmr"
>(Destructuring)</SPAN
></A
>. </P
><P CLASS="j"
>The iteration control clauses <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>for</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>as</SPAN
></SPAN
>, and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>repeat</SPAN
></SPAN
> must precede any other loop clauses, except <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>initially</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>with</SPAN
></SPAN
>, and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>named</SPAN
></SPAN
>, since they establish variable <A HREF="26_1_Glossary.html#binding"
><EM CLASS="term"
>bindings</EM
></A
>. When iteration control clauses are used in a <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
>, the corresponding termination tests in the loop body are evaluated before any other loop body code is executed. </P
><P CLASS="j"
>If multiple iteration clauses are used to control iteration, variable initialization and <EM CLASS="term"
>stepping</EM
><SPAN CLASS="meaning"
><SUB CLASS="sub"
><SPAN CLASS="cmr"
>1</SPAN
></SUB
></SPAN
> occur <A HREF="26_1_Glossary.html#sequentially"
><EM CLASS="term"
>sequentially</EM
></A
> by default. The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>and</SPAN
></SPAN
> construct can be used to connect two or more iteration clauses when <A HREF="26_1_Glossary.html#sequential"
><EM CLASS="term"
>sequential</EM
></A
> <A HREF="26_1_Glossary.html#binding"
><EM CLASS="term"
>binding</EM
></A
> and <EM CLASS="term"
>stepping</EM
><SPAN CLASS="meaning"
><SUB CLASS="sub"
><SPAN CLASS="cmr"
>1</SPAN
></SUB
></SPAN
> are not necessary. The iteration behavior of clauses joined by <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>and</SPAN
></SPAN
> is analogous to the behavior of the macro <A HREF="f_do.html" CLASS="macref"
><B
>do</B
></A
> with respect to <A HREF="f_do.html" CLASS="macref"
><B
>do*</B
></A
>. </P
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>for</SPAN
></SPAN
> and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>as</SPAN
></SPAN
> clauses iterate by using one or more local loop variables that are initialized to some value and that can be modified or <EM CLASS="term"
>stepped</EM
><SPAN CLASS="meaning"
><SUB CLASS="sub"
><SPAN CLASS="cmr"
>1</SPAN
></SUB
></SPAN
> 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 <EM CLASS="term"
>stepped</EM
><SPAN CLASS="meaning"
><SUB CLASS="sub"
><SPAN CLASS="cmr"
>1</SPAN
></SUB
></SPAN
> by an increment or a decrement or can be assigned a new value by the evaluation of a <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
>). Destructuring can be used to assign values to variables during iteration. </P
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>for</SPAN
></SPAN
> and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>as</SPAN
></SPAN
> keywords are synonyms; they can be used interchangeably. There are seven syntactic formats for these constructs. In each syntactic format, the <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> of <VAR CLASS="param"
>var</VAR
> can be supplied by the optional <VAR CLASS="param"
>type-spec</VAR
> argument. If <VAR CLASS="param"
>var</VAR
> is a destructuring list, the <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> supplied by the <VAR CLASS="param"
>type-spec</VAR
> argument must appropriately match the elements of the list. By convention, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>for</SPAN
></SPAN
> introduces new iterations and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>as</SPAN
></SPAN
> introduces iterations that depend on a previous iteration specification.</P
><H5 ID="sec_6_1_2_1_1" CLASS="HeadV"
>6.1.2.1.1 The for-as-arithmetic subclause</H5
><P CLASS="j"
>In the <I CLASS="i"
><I
>for-as-arithmetic</I
></I
> subclause, the <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>for</SPAN
></SPAN
> or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>as</SPAN
></SPAN
> construct iterates from the value supplied by <VAR CLASS="param"
>form1</VAR
> to the value supplied by <VAR CLASS="param"
>form2</VAR
> in increments or decrements denoted by <VAR CLASS="param"
>form3</VAR
>. Each expression is evaluated only once and must evaluate to a <A HREF="26_1_Glossary.html#number"
><EM CLASS="term"
>number</EM
></A
>. The variable <VAR CLASS="param"
>var</VAR
> is bound to the value of <VAR CLASS="param"
>form1</VAR
> in the first iteration and is <EM CLASS="term"
>stepped</EM
><SPAN CLASS="meaning"
><SUB CLASS="sub"
><SPAN CLASS="cmr"
>1</SPAN
></SUB
></SPAN
> by the value of <VAR CLASS="param"
>form3</VAR
> in each succeeding iteration, or by 1 if <VAR CLASS="param"
>form3</VAR
> is not provided. The following <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keywords</EM
></A
> serve as valid prepositions within this syntax. At least one of the prepositions must be used; and at most one from each line may be used in a single subclause.</P
><DL
><DT
><SPAN CLASS="cmtt"
>from</SPAN
> <SPAN CLASS="cmtt"
>|</SPAN
> <SPAN CLASS="cmtt"
>downfrom</SPAN
> <SPAN CLASS="cmtt"
>|</SPAN
> <SPAN CLASS="cmtt"
>upfrom</SPAN
></DT
><DT
><SPAN CLASS="cmtt"
>to</SPAN
> <SPAN CLASS="cmtt"
>|</SPAN
> <SPAN CLASS="cmtt"
>downto</SPAN
> <SPAN CLASS="cmtt"
>|</SPAN
> <SPAN CLASS="cmtt"
>upto</SPAN
> <SPAN CLASS="cmtt"
>|</SPAN
> <SPAN CLASS="cmtt"
>below</SPAN
> <SPAN CLASS="cmtt"
>|</SPAN
> <SPAN CLASS="cmtt"
>above</SPAN
></DT
><DT
><SPAN CLASS="cmtt"
>by</SPAN
></DT
></DL
><P CLASS="j"
>The prepositional phrases in each subclause may appear in any order. For example, either &#8220;<CODE CLASS="f"
>from x by y</CODE
>&#8221; or &#8220;<CODE CLASS="f"
>by y from x</CODE
>&#8221; is permitted. However, because left-to-right order of evaluation is preserved, the effects will be different in the case of side effects. <SPAN CLASS="idx" DATA-KIND="idxtext" DATA-TERM="order of evaluation"
></SPAN
><SPAN CLASS="idx" DATA-KIND="idxtext" DATA-TERM="evaluation order"
></SPAN
>Consider:</P
><PRE CLASS="screen"
>(let ((x 1)) (loop for i from x by (incf x) to 10 collect i))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (1 3 5 7 9)
(let ((x 1)) (loop for i by (incf x) from x to 10 collect i))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (2 4 6 8 10)</PRE
><P CLASS="j"
>The descriptions of the prepositions follow:</P
><DL
><DT
><SPAN CLASS="cmtt"
>from</SPAN
></DT
><DD
><P CLASS="j"
>The <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keyword</EM
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>from</SPAN
></SPAN
> specifies the value from which <EM CLASS="term"
>stepping</EM
><SPAN CLASS="meaning"
><SUB CLASS="sub"
><SPAN CLASS="cmr"
>1</SPAN
></SUB
></SPAN
> begins, as supplied by <VAR CLASS="param"
>form1</VAR
>. <EM CLASS="term"
>Stepping</EM
><SPAN CLASS="meaning"
><SUB CLASS="sub"
><SPAN CLASS="cmr"
>1</SPAN
></SUB
></SPAN
> is incremental by default. If decremental <EM CLASS="term"
>stepping</EM
><SPAN CLASS="meaning"
><SUB CLASS="sub"
><SPAN CLASS="cmr"
>1</SPAN
></SUB
></SPAN
> is desired, the preposition <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>downto</SPAN
></SPAN
> or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>above</SPAN
></SPAN
> must be used with <VAR CLASS="param"
>form2</VAR
>. For incremental <EM CLASS="term"
>stepping</EM
><SPAN CLASS="meaning"
><SUB CLASS="sub"
><SPAN CLASS="cmr"
>1</SPAN
></SUB
></SPAN
>, the default <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>from</SPAN
></SPAN
> value is 0.</P
></DD
><DT
><SPAN CLASS="cmtt"
>downfrom,</SPAN
> <SPAN CLASS="cmtt"
>upfrom</SPAN
></DT
><DD
><P CLASS="j"
>The <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keyword</EM
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>downfrom</SPAN
></SPAN
> indicates that the variable <VAR CLASS="param"
>var</VAR
> is decreased in decrements supplied by <VAR CLASS="param"
>form3</VAR
>; the <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keyword</EM
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>upfrom</SPAN
></SPAN
> indicates that <VAR CLASS="param"
>var</VAR
> is increased in increments supplied by <VAR CLASS="param"
>form3</VAR
>.</P
></DD
><DT
><SPAN CLASS="cmtt"
>to</SPAN
></DT
><DD
><P CLASS="j"
>The <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keyword</EM
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>to</SPAN
></SPAN
> marks the end value for <EM CLASS="term"
>stepping</EM
><SPAN CLASS="meaning"
><SUB CLASS="sub"
><SPAN CLASS="cmr"
>1</SPAN
></SUB
></SPAN
> supplied in <VAR CLASS="param"
>form2</VAR
>. <EM CLASS="term"
>Stepping</EM
><SPAN CLASS="meaning"
><SUB CLASS="sub"
><SPAN CLASS="cmr"
>1</SPAN
></SUB
></SPAN
> is incremental by default. If decremental <EM CLASS="term"
>stepping</EM
><SPAN CLASS="meaning"
><SUB CLASS="sub"
><SPAN CLASS="cmr"
>1</SPAN
></SUB
></SPAN
> is desired, the preposition <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>downfrom</SPAN
></SPAN
> must be used with <VAR CLASS="param"
>form1</VAR
>, or else the preposition <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>downto</SPAN
></SPAN
> or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>above</SPAN
></SPAN
> should be used instead of <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>to</SPAN
></SPAN
> with <VAR CLASS="param"
>form2</VAR
>.</P
></DD
><DT
><SPAN CLASS="cmtt"
>downto,</SPAN
> <SPAN CLASS="cmtt"
>upto</SPAN
></DT
><DD
><P CLASS="j"
>The <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keyword</EM
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>downto</SPAN
></SPAN
> specifies decremental <EM CLASS="term"
>stepping</EM
>; the <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keyword</EM
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>upto</SPAN
></SPAN
> specifies incremental <EM CLASS="term"
>stepping</EM
>. In both cases, the amount of change on each step is specified by <VAR CLASS="param"
>form3</VAR
>, and the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> terminates when the variable <VAR CLASS="param"
>var</VAR
> passes the value of <VAR CLASS="param"
>form2</VAR
>. Since there is no default for <VAR CLASS="param"
>form1</VAR
> in decremental <EM CLASS="term"
>stepping</EM
><SPAN CLASS="meaning"
><SUB CLASS="sub"
><SPAN CLASS="cmr"
>1</SPAN
></SUB
></SPAN
>, a <VAR CLASS="param"
>form1</VAR
> value must be supplied (using <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>from</SPAN
></SPAN
> or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>downfrom</SPAN
></SPAN
>) when <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>downto</SPAN
></SPAN
> is supplied.</P
></DD
><DT
><SPAN CLASS="cmtt"
>below,</SPAN
> <SPAN CLASS="cmtt"
>above</SPAN
></DT
><DD
><P CLASS="j"
>The <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keywords</EM
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>below</SPAN
></SPAN
> and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>above</SPAN
></SPAN
> are analogous to <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>upto</SPAN
></SPAN
> and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>downto</SPAN
></SPAN
> respectively. These keywords stop iteration just before the value of the variable <VAR CLASS="param"
>var</VAR
> reaches the value supplied by <VAR CLASS="param"
>form2</VAR
>; the end value of <VAR CLASS="param"
>form2</VAR
> is not included. Since there is no default for <VAR CLASS="param"
>form1</VAR
> in decremental <EM CLASS="term"
>stepping</EM
><SPAN CLASS="meaning"
><SUB CLASS="sub"
><SPAN CLASS="cmr"
>1</SPAN
></SUB
></SPAN
>, a <VAR CLASS="param"
>form1</VAR
> value must be supplied (using <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>from</SPAN
></SPAN
> or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>downfrom</SPAN
></SPAN
>) when <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>above</SPAN
></SPAN
> is supplied.</P
></DD
><DT
><SPAN CLASS="cmtt"
>by</SPAN
></DT
><DD
><P CLASS="j"
>The <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keyword</EM
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>by</SPAN
></SPAN
> marks the increment or decrement supplied by <VAR CLASS="param"
>form3</VAR
>. The value of <VAR CLASS="param"
>form3</VAR
> can be any positive <A HREF="26_1_Glossary.html#number"
><EM CLASS="term"
>number</EM
></A
>. The default value is 1.</P
></DD
></DL
><P CLASS="j"
>In an iteration control clause, the <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>for</SPAN
></SPAN
> or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>as</SPAN
></SPAN
> construct causes termination when the supplied limit is reached. That is, iteration continues until the value <VAR CLASS="param"
>var</VAR
> is stepped to the exclusive or inclusive limit supplied by <VAR CLASS="param"
>form2</VAR
>. The range is exclusive if <VAR CLASS="param"
>form3</VAR
> increases or decreases <VAR CLASS="param"
>var</VAR
> to the value of <VAR CLASS="param"
>form2</VAR
> without reaching that value; the loop keywords <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>below</SPAN
></SPAN
> and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>above</SPAN
></SPAN
> provide exclusive limits. An inclusive limit allows <VAR CLASS="param"
>var</VAR
> to attain the value of <VAR CLASS="param"
>form2</VAR
>; <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>to</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>downto</SPAN
></SPAN
>, and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>upto</SPAN
></SPAN
> provide inclusive limits.</P
><H6 ID="sec_6_1_2_1_1_1" CLASS="HeadVI"
>6.1.2.1.1.1 Examples of for-as-arithmetic subclause</H6
><PRE CLASS="screen"
>;; Print some numbers.
(loop for i from 1 to 3
do (print i))
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 1
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 2
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 3
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> NIL
;; Print every third number.
(loop for i from 10 downto 1 by 3
do (print i))
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 10
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 7
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 4
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 1
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> NIL
;; Step incrementally from the default starting value.
(loop for i below 3
do (print i))
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 0
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 1
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 2
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> NIL</PRE
><H5 ID="sec_6_1_2_1_2" CLASS="HeadV"
>6.1.2.1.2 The for-as-in-list subclause</H5
><P CLASS="j"
>In the <I CLASS="i"
><I
>for-as-in-list</I
></I
> subclause, the <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>for</SPAN
></SPAN
> or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>as</SPAN
></SPAN
> construct iterates over the contents of a <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
>. It checks for the end of the <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
> as if by using <A HREF="f_endp.html" CLASS="funref"
><B
>endp</B
></A
>. The variable <VAR CLASS="param"
>var</VAR
> is bound to the successive elements of the <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
> in <VAR CLASS="param"
>form1</VAR
> before each iteration. At the end of each iteration, the function <VAR CLASS="param"
>step-fun</VAR
> is applied to the <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
>; the default value for <VAR CLASS="param"
>step-fun</VAR
> is <A HREF="f_car.html" CLASS="funref"
><B
>cdr</B
></A
>. The <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keywords</EM
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>in</SPAN
></SPAN
> and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>by</SPAN
></SPAN
> serve as valid prepositions in this syntax. The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>for</SPAN
></SPAN
> or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>as</SPAN
></SPAN
> construct causes termination when the end of the <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
> is reached.</P
><H6 ID="sec_6_1_2_1_2_1" CLASS="HeadVI"
>6.1.2.1.2.1 Examples of for-as-in-list subclause</H6
><PRE CLASS="screen"
>;; Print every item in a list.
(loop for item in '(1 2 3) do (print item))
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 1
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 2
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 3
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> NIL
;; Print every other item in a list.
(loop for item in '(1 2 3 4 5) by #'cddr
do (print item))
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 1
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 3
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 5
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> NIL
;; Destructure a list, and sum the x values using fixnum arithmetic.
(loop for (item . x) of-type (t . fixnum) in '((A . 1) (B . 2) (C . 3))
unless (eq item 'B) sum x)
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 4</PRE
><H5 ID="sec_6_1_2_1_3" CLASS="HeadV"
>6.1.2.1.3 The for-as-on-list subclause</H5
><P CLASS="j"
>In the <I CLASS="i"
><I
>for-as-on-list</I
></I
> subclause, the <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>for</SPAN
></SPAN
> or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>as</SPAN
></SPAN
> construct iterates over a <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
>. It checks for the end of the <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
> as if by using <A HREF="f_atom.html" CLASS="funref"
><B
>atom</B
></A
>. The variable <VAR CLASS="param"
>var</VAR
> is bound to the successive tails of the <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
> in <VAR CLASS="param"
>form1</VAR
>. At the end of each iteration, the function <VAR CLASS="param"
>step-fun</VAR
> is applied to the <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
>; the default value for <VAR CLASS="param"
>step-fun</VAR
> is <A HREF="f_car.html" CLASS="funref"
><B
>cdr</B
></A
>. The <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keywords</EM
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>on</SPAN
></SPAN
> and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>by</SPAN
></SPAN
> serve as valid prepositions in this syntax. The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>for</SPAN
></SPAN
> or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>as</SPAN
></SPAN
> construct causes termination when the end of the <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
> is reached.</P
><H6 ID="sec_6_1_2_1_3_1" CLASS="HeadVI"
>6.1.2.1.3.1 Examples of for-as-on-list subclause</H6
><PRE CLASS="screen"
>;; Collect successive tails of a list.
(loop for sublist on '(a b c d)
collect sublist)
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> ((A B C D) (B C D) (C D) (D))
;; Print a list by using destructuring with the loop keyword ON.
(loop for (item) on '(1 2 3)
do (print item))
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 1
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 2
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 3
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> NIL</PRE
><H5 ID="sec_6_1_2_1_4" CLASS="HeadV"
>6.1.2.1.4 The for-as-equals-then subclause</H5
><P CLASS="j"
>In the <I CLASS="i"
><I
>for-as-equals-then</I
></I
> subclause the <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>for</SPAN
></SPAN
> or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>as</SPAN
></SPAN
> construct initializes the variable <VAR CLASS="param"
>var</VAR
> by setting it to the result of evaluating <VAR CLASS="param"
>form1</VAR
> on the first iteration, then setting it to the result of evaluating <VAR CLASS="param"
>form2</VAR
> on the second and subsequent iterations. If <VAR CLASS="param"
>form2</VAR
> is omitted, the construct uses <VAR CLASS="param"
>form1</VAR
> on the second and subsequent iterations. The <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keywords</EM
></A
> = and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>then</SPAN
></SPAN
> serve as valid prepositions in this syntax. This construct does not provide any termination tests.</P
><H6 ID="sec_6_1_2_1_4_1" CLASS="HeadVI"
>6.1.2.1.4.1 Examples of for-as-equals-then subclause</H6
><PRE CLASS="screen"
>;; Collect some numbers.
(loop for item = 1 then (+ item 10)
for iteration from 1 to 5
collect item)
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (1 11 21 31 41)</PRE
><H5 ID="sec_6_1_2_1_5" CLASS="HeadV"
>6.1.2.1.5 The for-as-across subclause</H5
><P CLASS="j"
>In the <I CLASS="i"
><I
>for-as-across</I
></I
> subclause the <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>for</SPAN
></SPAN
> or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>as</SPAN
></SPAN
> construct binds the variable <VAR CLASS="param"
>var</VAR
> to the value of each element in the array <VAR CLASS="param"
>vector</VAR
>. The <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keyword</EM
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>across</SPAN
></SPAN
> marks the array <VAR CLASS="param"
>vector</VAR
>; <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>across</SPAN
></SPAN
> is used as a preposition in this syntax. Iteration stops when there are no more elements in the supplied <A HREF="26_1_Glossary.html#array"
><EM CLASS="term"
>array</EM
></A
> that can be referenced. Some implementations might recognize a <A HREF="f_the.html" CLASS="specref"
><B
>the</B
></A
> special form in the <VAR CLASS="param"
>vector</VAR
> form to produce more efficient code.</P
><H6 ID="sec_6_1_2_1_5_1" CLASS="HeadVI"
>6.1.2.1.5.1 Examples of for-as-across subclause</H6
><PRE CLASS="screen"
>(loop for char across (the simple-string (find-message channel))
do (write-char char stream))</PRE
><H5 ID="sec_6_1_2_1_6" CLASS="HeadV"
>6.1.2.1.6 The for-as-hash subclause</H5
><P CLASS="j"
>In the <I CLASS="i"
><I
>for-as-hash</I
></I
> subclause the <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>for</SPAN
></SPAN
> or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>as</SPAN
></SPAN
> construct iterates over the elements, keys, and values of a <EM CLASS="term"
>hash-table</EM
>. In this syntax, a compound preposition is used to designate access to a <A HREF="26_1_Glossary.html#hash_table"
><EM CLASS="term"
>hash table</EM
></A
>. The variable <VAR CLASS="param"
>var</VAR
> takes on the value of each hash key or hash value in the supplied <VAR CLASS="param"
>hash-table</VAR
>. The following <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keywords</EM
></A
> serve as valid prepositions within this syntax:</P
><DL
><DT
><SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>being</SPAN
></SPAN
></DT
><DD
><P CLASS="j"
>The keyword <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>being</SPAN
></SPAN
> introduces either the Loop schema <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>hash-key</SPAN
></SPAN
> or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>hash-value</SPAN
></SPAN
>.</P
></DD
><DT
><SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>each</SPAN
></SPAN
><SPAN CLASS="cmr"
>,</SPAN
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>the</SPAN
></SPAN
></DT
><DD
><P CLASS="j"
>The <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keyword</EM
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>each</SPAN
></SPAN
> follows the <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keyword</EM
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>being</SPAN
></SPAN
> when <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>hash-key</SPAN
></SPAN
> or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>hash-value</SPAN
></SPAN
> is used. The <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keyword</EM
></A
> <SPAN CLASS="cmtt"
>the</SPAN
> is used with <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>hash-keys</SPAN
></SPAN
> and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>hash-values</SPAN
></SPAN
> only for ease of reading. This agreement isn&#8217;t required.</P
></DD
><DT
><SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>hash-key</SPAN
></SPAN
><SPAN CLASS="cmr"
>,</SPAN
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>hash-keys</SPAN
></SPAN
></DT
><DD
><P CLASS="j"
>These <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keywords</EM
></A
> access each key entry of the <A HREF="26_1_Glossary.html#hash_table"
><EM CLASS="term"
>hash table</EM
></A
>. If the name <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>hash-value</SPAN
></SPAN
> is supplied in a <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>using</SPAN
></SPAN
> construct with one of these Loop schemas, the iteration can optionally access the keyed value. The order in which the keys are accessed is undefined; empty slots in the <A HREF="26_1_Glossary.html#hash_table"
><EM CLASS="term"
>hash table</EM
></A
> are ignored.</P
></DD
><DT
><SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>hash-value</SPAN
></SPAN
><SPAN CLASS="cmr"
>,</SPAN
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>hash-values</SPAN
></SPAN
></DT
><DD
><P CLASS="j"
>These <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keywords</EM
></A
> access each value entry of a <A HREF="26_1_Glossary.html#hash_table"
><EM CLASS="term"
>hash table</EM
></A
>. If the name <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>hash-key</SPAN
></SPAN
> is supplied in a <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>using</SPAN
></SPAN
> construct with one of these Loop schemas, the iteration can optionally access the key that corresponds to the value. The order in which the keys are accessed is undefined; empty slots in the <A HREF="26_1_Glossary.html#hash_table"
><EM CLASS="term"
>hash table</EM
></A
> are ignored.</P
></DD
><DT
><SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>using</SPAN
></SPAN
></DT
><DD
><P CLASS="j"
>The <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keyword</EM
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>using</SPAN
></SPAN
> introduces the optional key or the keyed value to be accessed. It allows access to the hash key if iteration is over the hash values, and the hash value if iteration is over the hash keys.</P
></DD
><DT
><SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>in</SPAN
></SPAN
><SPAN CLASS="cmr"
>,</SPAN
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>of</SPAN
></SPAN
></DT
><DD
><P CLASS="j"
>These loop prepositions introduce <VAR CLASS="param"
>hash-table</VAR
>.</P
></DD
></DL
><P CLASS="j"
>In effect </P
><P CLASS="j"
><SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>being</SPAN
></SPAN
> {<SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>each</SPAN
></SPAN
> | <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>the</SPAN
></SPAN
>} {<SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>hash-value</SPAN
></SPAN
> | <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>hash-values</SPAN
></SPAN
> | <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>hash-key</SPAN
></SPAN
> | <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>hash-keys</SPAN
></SPAN
>} {<SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>in</SPAN
></SPAN
> | <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>of</SPAN
></SPAN
>} </P
><P CLASS="j"
>is a compound preposition. </P
><P CLASS="j"
>Iteration stops when there are no more hash keys or hash values to be referenced in the supplied <VAR CLASS="param"
>hash-table</VAR
>.</P
><H5 ID="sec_6_1_2_1_7" CLASS="HeadV"
>6.1.2.1.7 The for-as-package subclause</H5
><P CLASS="j"
>In the <I CLASS="i"
><I
>for-as-package</I
></I
> subclause the <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>for</SPAN
></SPAN
> or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>as</SPAN
></SPAN
> construct iterates over the <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbols</EM
></A
> in a <A HREF="26_1_Glossary.html#package"
><EM CLASS="term"
>package</EM
></A
>. In this syntax, a compound preposition is used to designate access to a <A HREF="26_1_Glossary.html#package"
><EM CLASS="term"
>package</EM
></A
>. The variable <VAR CLASS="param"
>var</VAR
> takes on the value of each <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbol</EM
></A
> in the supplied <A HREF="26_1_Glossary.html#package"
><EM CLASS="term"
>package</EM
></A
>. The following <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keywords</EM
></A
> serve as valid prepositions within this syntax:</P
><DL
><DT
><SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>being</SPAN
></SPAN
></DT
><DD
><P CLASS="j"
>The keyword <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>being</SPAN
></SPAN
> introduces either the Loop schema <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>symbol</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>present-symbol</SPAN
></SPAN
>, or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>external-symbol</SPAN
></SPAN
>.</P
></DD
><DT
><SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>each</SPAN
></SPAN
><SPAN CLASS="cmr"
>,</SPAN
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>the</SPAN
></SPAN
></DT
><DD
><P CLASS="j"
>The <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keyword</EM
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>each</SPAN
></SPAN
> follows the <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keyword</EM
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>being</SPAN
></SPAN
> when <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>symbol</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>present-symbol</SPAN
></SPAN
>, or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>external-symbol</SPAN
></SPAN
> is used. The <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keyword</EM
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>the</SPAN
></SPAN
> is used with <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>symbols</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>present-symbols</SPAN
></SPAN
>, and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>external-symbols</SPAN
></SPAN
> only for ease of reading. This agreement isn&#8217;t required.</P
></DD
><DT
><SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>present-symbol</SPAN
></SPAN
><SPAN CLASS="cmr"
>,</SPAN
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>present-symbols</SPAN
></SPAN
></DT
><DD
><P CLASS="j"
>These Loop schemas iterate over the <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbols</EM
></A
> that are <A HREF="26_1_Glossary.html#present"
><EM CLASS="term"
>present</EM
></A
> in a <A HREF="26_1_Glossary.html#package"
><EM CLASS="term"
>package</EM
></A
>. The <VAR CLASS="param"
>package</VAR
> to be iterated over is supplied in the same way that <A HREF="26_1_Glossary.html#package"
><EM CLASS="term"
>package</EM
></A
> arguments to <A HREF="f_find-package.html" CLASS="funref"
><B
>find-package</B
></A
> are supplied. If the <VAR CLASS="param"
>package</VAR
> for the iteration is not supplied, the <A HREF="26_1_Glossary.html#current_package"
><EM CLASS="term"
>current package</EM
></A
> is used. If a <VAR CLASS="param"
>package</VAR
> that does not exist is supplied, an error of <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> <A HREF="t_package-error.html" CLASS="typeref"
><B
>package-error</B
></A
> is signaled.</P
></DD
><DT
><SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>symbol</SPAN
></SPAN
><SPAN CLASS="cmr"
>,</SPAN
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>symbols</SPAN
></SPAN
></DT
><DD
><P CLASS="j"
>These Loop schemas iterate over <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbols</EM
></A
> that are <A HREF="26_1_Glossary.html#accessible"
><EM CLASS="term"
>accessible</EM
></A
> in a given <VAR CLASS="param"
>package</VAR
>. The <VAR CLASS="param"
>package</VAR
> to be iterated over is supplied in the same way that <A HREF="26_1_Glossary.html#package"
><EM CLASS="term"
>package</EM
></A
> arguments to <A HREF="f_find-package.html" CLASS="funref"
><B
>find-package</B
></A
> are supplied. If the <VAR CLASS="param"
>package</VAR
> for the iteration is not supplied, the <A HREF="26_1_Glossary.html#current_package"
><EM CLASS="term"
>current package</EM
></A
> is used. If a <VAR CLASS="param"
>package</VAR
> that does not exist is supplied, an error of <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> <A HREF="t_package-error.html" CLASS="typeref"
><B
>package-error</B
></A
> is signaled.</P
></DD
><DT
><SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>external-symbol</SPAN
></SPAN
><SPAN CLASS="cmr"
>,</SPAN
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>external-symbols</SPAN
></SPAN
></DT
><DD
><P CLASS="j"
>These Loop schemas iterate over the <A HREF="26_1_Glossary.html#external_symbol"
><EM CLASS="term"
>external symbols</EM
></A
> of a <VAR CLASS="param"
>package</VAR
>. The <VAR CLASS="param"
>package</VAR
> to be iterated over is supplied in the same way that <A HREF="26_1_Glossary.html#package"
><EM CLASS="term"
>package</EM
></A
> arguments to <A HREF="f_find-package.html" CLASS="funref"
><B
>find-package</B
></A
> are supplied. If the <VAR CLASS="param"
>package</VAR
> for the iteration is not supplied, the <A HREF="26_1_Glossary.html#current_package"
><EM CLASS="term"
>current package</EM
></A
> is used. If a <VAR CLASS="param"
>package</VAR
> that does not exist is supplied, an error of <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> <A HREF="t_package-error.html" CLASS="typeref"
><B
>package-error</B
></A
> is signaled.</P
></DD
><DT
><SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>in</SPAN
></SPAN
><SPAN CLASS="cmr"
>,</SPAN
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>of</SPAN
></SPAN
></DT
><DD
><P CLASS="j"
>These loop prepositions introduce <VAR CLASS="param"
>package</VAR
>.</P
></DD
></DL
><P CLASS="j"
>In effect </P
><P CLASS="j"
><SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>being</SPAN
></SPAN
> {<SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>each</SPAN
></SPAN
> | <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>the</SPAN
></SPAN
>} {<SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>symbol</SPAN
></SPAN
> | <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>symbols</SPAN
></SPAN
> | <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>present-symbol</SPAN
></SPAN
> | <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>present-symbols</SPAN
></SPAN
> | <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>external-symbol</SPAN
></SPAN
> | <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>external-symbols</SPAN
></SPAN
>} {<SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>in</SPAN
></SPAN
> | <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>of</SPAN
></SPAN
>} </P
><P CLASS="j"
>is a compound preposition. </P
><P CLASS="j"
>Iteration stops when there are no more <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbols</EM
></A
> to be referenced in the supplied <VAR CLASS="param"
>package</VAR
>.</P
><H6 ID="sec_6_1_2_1_7_1" CLASS="HeadVI"
>6.1.2.1.7.1 Examples of for-as-package subclause</H6
><PRE CLASS="screen"
> (let ((*package* (make-package "TEST-PACKAGE-1")))
;; For effect, intern some symbols
(read-from-string "(THIS IS A TEST)")
(export (intern "THIS"))
(loop for x being each present-symbol of *package*
do (print x)))
<SPAN CLASS="cmr"
>&#8883;</SPAN
> A
<SPAN CLASS="cmr"
>&#8883;</SPAN
> TEST
<SPAN CLASS="cmr"
>&#8883;</SPAN
> THIS
<SPAN CLASS="cmr"
>&#8883;</SPAN
> IS
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> NIL</PRE
><H4 ID="sec_6_1_2_2" CLASS="HeadIV"
>6.1.2.2 Local Variable Initializations</H4
><P CLASS="j"
>When a <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> is executed, the local variables are bound and are initialized to some value. These local variables exist until <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> iteration terminates, at which point they cease to exist. Implicit variables are also established by iteration control clauses and the <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>into</SPAN
></SPAN
> preposition of accumulation clauses. </P
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>with</SPAN
></SPAN
> construct initializes variables that are local to a loop. The variables are initialized one time only. If the optional <VAR CLASS="param"
>type-spec</VAR
> argument is supplied for the variable <VAR CLASS="param"
>var</VAR
>, but there is no related expression to be evaluated, <VAR CLASS="param"
>var</VAR
> is initialized to an appropriate default value for its <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
>. For example, for the types <A HREF="t_t.html" CLASS="typeref"
><B
>t</B
></A
>, <A HREF="t_number.html" CLASS="typeref"
><B
>number</B
></A
>, and <A HREF="t_float.html" CLASS="typeref"
><B
>float</B
></A
>, the default values are <SPAN CLASS="misc"
><B
>nil</B
></SPAN
>, <CODE CLASS="f"
>0</CODE
>, and <CODE CLASS="f"
>0.0</CODE
> respectively. The consequences are undefined if a <VAR CLASS="param"
>type-spec</VAR
> argument is supplied for <VAR CLASS="param"
>var</VAR
> if the related expression returns a value that is not of the supplied <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
>. By default, the <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>with</SPAN
></SPAN
> construct initializes variables <A HREF="26_1_Glossary.html#sequentially"
><EM CLASS="term"
>sequentially</EM
></A
>; that is, one variable is assigned a value before the next expression is evaluated. However, by using the <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keyword</EM
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>and</SPAN
></SPAN
> to join several <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>with</SPAN
></SPAN
> clauses, initializations can be forced to occur in <A HREF="26_1_Glossary.html#parallel"
><EM CLASS="term"
>parallel</EM
></A
>; that is, all of the supplied <VAR CLASS="param"
>forms</VAR
> are evaluated, and the results are bound to the respective variables simultaneously. </P
><P CLASS="j"
><A HREF="26_1_Glossary.html#sequential"
><EM CLASS="term"
>Sequential</EM
></A
> <A HREF="26_1_Glossary.html#binding"
><EM CLASS="term"
>binding</EM
></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 <CODE CLASS="f"
>a</CODE
>, <CODE CLASS="f"
>b</CODE
>, and <CODE CLASS="f"
>c</CODE
> are to be bound in sequence:</P
><PRE CLASS="screen"
> (loop with a = 1
with b = (+ a 2)
with c = (+ b 3)
return (list a b c))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (1 3 6)</PRE
><P CLASS="j"
>The execution of the above <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> is equivalent to the execution of the following code:</P
><PRE CLASS="screen"
>(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
><P CLASS="j"
>If the values of previously bound variables are not needed for the initialization of other local variables, an <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>and</SPAN
></SPAN
> clause can be used to specify that the bindings are to occur in <A HREF="26_1_Glossary.html#parallel"
><EM CLASS="term"
>parallel</EM
></A
>:</P
><PRE CLASS="screen"
> (loop with a = 1
and b = 2
and c = 3
return (list a b c))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (1 2 3)</PRE
><P CLASS="j"
>The execution of the above loop is equivalent to the execution of the following code:</P
><PRE CLASS="screen"
>(block nil
(let ((a 1)
(b 2)
(c 3))
(tagbody
(next-loop (return (list a b c))
(go next-loop)
end-loop))))</PRE
><H5 ID="sec_6_1_2_2_1" CLASS="HeadV"
>6.1.2.2.1 Examples of WITH clause</H5
><PRE CLASS="screen"
>;; These bindings occur in sequence.
(loop with a = 1
with b = (+ a 2)
with c = (+ b 3)
return (list a b c))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (1 3 6)
;; These bindings occur in parallel.
(setq a 5 b 10)
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 10
(loop with a = 1
and b = (+ a 2)
and c = (+ b 3)
return (list a b c))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (1 7 13)
;; This example shows a shorthand way to declare local variables
;; that are of different types.
(loop with (a b c) of-type (float integer float)
return (format nil "~A ~A ~A" a b c))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> "0.0 0 0.0"
;; This example shows a shorthand way to declare local variables
;; that are the same type.
(loop with (a b c) of-type float
return (format nil "~A ~A ~A" a b c))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> "0.0 0.0 0.0"</PRE
><H3 ID="sec_6_1_3" CLASS="HeadIII"
>6.1.3 Value Accumulation Clauses</H3
><P CLASS="j"
>The constructs <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>collect</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>collecting</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>append</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>appending</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>nconc</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>nconcing</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>count</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>counting</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>maximize</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>maximizing</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>minimize</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>minimizing</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>sum</SPAN
></SPAN
>, and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>summing</SPAN
></SPAN
>, allow values to be accumulated in a <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
>. </P
><P CLASS="j"
>The constructs <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>collect</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>collecting</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>append</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>appending</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>nconc</SPAN
></SPAN
>, and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>nconcing</SPAN
></SPAN
>, designate clauses that accumulate values in <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>lists</EM
></A
> and return them. The constructs <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>count</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>counting</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>maximize</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>maximizing</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>minimize</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>minimizing</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>sum</SPAN
></SPAN
>, and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>summing</SPAN
></SPAN
> designate clauses that accumulate and return numerical values. </P
><P CLASS="j"
>During each iteration, the constructs <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>collect</SPAN
></SPAN
> and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>collecting</SPAN
></SPAN
> collect the value of the supplied <VAR CLASS="param"
>form</VAR
> into a <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
>. When iteration terminates, the <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
> is returned. The argument <VAR CLASS="param"
>var</VAR
> is set to the <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
> of collected values; if <VAR CLASS="param"
>var</VAR
> is supplied, the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> does not return the final <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
> automatically. If <VAR CLASS="param"
>var</VAR
> is not supplied, it is equivalent to supplying an internal name for <VAR CLASS="param"
>var</VAR
> and returning its value in a <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>finally</SPAN
></SPAN
> clause. The <VAR CLASS="param"
>var</VAR
> argument is bound as if by the construct <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>with</SPAN
></SPAN
>. No mechanism is provided for declaring the <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> of <VAR CLASS="param"
>var</VAR
>; it must be of <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> <A HREF="t_list.html" CLASS="typeref"
><B
>list</B
></A
>. </P
><P CLASS="j"
>The constructs <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>append</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>appending</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>nconc</SPAN
></SPAN
>, and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>nconcing</SPAN
></SPAN
> are similar to <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>collect</SPAN
></SPAN
> except that the values of the supplied <VAR CLASS="param"
>form</VAR
> must be <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>lists</EM
></A
>.</P
><UL
><LI
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>append</SPAN
></SPAN
> keyword causes its <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
> values to be concatenated into a single <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
>, as if they were arguments to the <A HREF="26_1_Glossary.html#function"
><EM CLASS="term"
>function</EM
></A
> <A HREF="f_append.html" CLASS="funref"
><B
>append</B
></A
>.</P
></LI
><LI
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>nconc</SPAN
></SPAN
> keyword causes its <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
> values to be concatenated into a single <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
>, as if they were arguments to the <A HREF="26_1_Glossary.html#function"
><EM CLASS="term"
>function</EM
></A
> <A HREF="f_nconc.html" CLASS="funref"
><B
>nconc</B
></A
>.</P
></LI
></UL
><P CLASS="j"
>The argument <VAR CLASS="param"
>var</VAR
> is set to the <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
> of concatenated values; if <VAR CLASS="param"
>var</VAR
> is supplied, <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> does not return the final <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
> automatically. The <VAR CLASS="param"
>var</VAR
> argument is bound as if by the construct <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>with</SPAN
></SPAN
>. A <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> cannot be supplied for <VAR CLASS="param"
>var</VAR
>; it must be of <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> <A HREF="t_list.html" CLASS="typeref"
><B
>list</B
></A
>. The construct <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>nconc</SPAN
></SPAN
> destructively modifies its argument <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>lists</EM
></A
>. </P
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>count</SPAN
></SPAN
> construct counts the number of times that the supplied <VAR CLASS="param"
>form</VAR
> returns <A HREF="26_1_Glossary.html#true"
><EM CLASS="term"
>true</EM
></A
>. The argument <VAR CLASS="param"
>var</VAR
> accumulates the number of occurrences; if <VAR CLASS="param"
>var</VAR
> is supplied, <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> does not return the final count automatically. The <VAR CLASS="param"
>var</VAR
> argument is bound as if by the construct <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>with</SPAN
></SPAN
> to a zero of the appropriate type. Subsequent values (including any necessary coercions) are computed as if by the function <A HREF="f_1_plus.html" CLASS="funref"
><B
>1+</B
></A
>. If <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>into</SPAN
></SPAN
> <VAR CLASS="param"
>var</VAR
> is used, a <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> can be supplied for <VAR CLASS="param"
>var</VAR
> with the <VAR CLASS="param"
>type-spec</VAR
> argument; the consequences are unspecified if a nonnumeric <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> is supplied. If there is no <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>into</SPAN
></SPAN
> variable, the optional <VAR CLASS="param"
>type-spec</VAR
> argument applies to the internal variable that is keeping the count. The default <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> is <A HREF="26_1_Glossary.html#implementation-dependent"
><EM CLASS="term"
>implementation-dependent</EM
></A
>; but it must be a <A HREF="26_1_Glossary.html#supertype"
><EM CLASS="term"
>supertype</EM
></A
> of <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> <A HREF="t_fixnum.html" CLASS="typeref"
><B
>fixnum</B
></A
>. </P
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>maximize</SPAN
></SPAN
> and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>minimize</SPAN
></SPAN
> constructs compare the value of the supplied <VAR CLASS="param"
>form</VAR
> obtained during the first iteration with values obtained in successive iterations. The maximum (for <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>maximize</SPAN
></SPAN
>) or minimum (for <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>minimize</SPAN
></SPAN
>) value encountered is determined (as if by the <A HREF="26_1_Glossary.html#function"
><EM CLASS="term"
>function</EM
></A
> <A HREF="f_max.html" CLASS="funref"
><B
>max</B
></A
> for <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>maximize</SPAN
></SPAN
> and as if by the <A HREF="26_1_Glossary.html#function"
><EM CLASS="term"
>function</EM
></A
> <A HREF="f_max.html" CLASS="funref"
><B
>min</B
></A
> for <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>minimize</SPAN
></SPAN
>) and returned. If the <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>maximize</SPAN
></SPAN
> or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>minimize</SPAN
></SPAN
> clause is never executed, the accumulated value is unspecified. The argument <VAR CLASS="param"
>var</VAR
> accumulates the maximum or minimum value; if <VAR CLASS="param"
>var</VAR
> is supplied, <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> does not return the maximum or minimum automatically. The <VAR CLASS="param"
>var</VAR
> argument is bound as if by the construct <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>with</SPAN
></SPAN
>. If <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>into</SPAN
></SPAN
> <VAR CLASS="param"
>var</VAR
> is used, a <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> can be supplied for <VAR CLASS="param"
>var</VAR
> with the <VAR CLASS="param"
>type-spec</VAR
> argument; the consequences are unspecified if a nonnumeric <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> is supplied. If there is no <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>into</SPAN
></SPAN
> variable, the optional <VAR CLASS="param"
>type-spec</VAR
> argument applies to the internal variable that is keeping the maximum or minimum value. The default <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> is <A HREF="26_1_Glossary.html#implementation-dependent"
><EM CLASS="term"
>implementation-dependent</EM
></A
>; but it must be a <A HREF="26_1_Glossary.html#supertype"
><EM CLASS="term"
>supertype</EM
></A
> of <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> <A HREF="t_real.html" CLASS="typeref"
><B
>real</B
></A
>. </P
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>sum</SPAN
></SPAN
> construct forms a cumulative sum of the successive <A HREF="26_1_Glossary.html#primary_value"
><EM CLASS="term"
>primary values</EM
></A
> of the supplied <VAR CLASS="param"
>form</VAR
> at each iteration. The argument <VAR CLASS="param"
>var</VAR
> is used to accumulate the sum; if <VAR CLASS="param"
>var</VAR
> is supplied, <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> does not return the final sum automatically. The <VAR CLASS="param"
>var</VAR
> argument is bound as if by the construct <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>with</SPAN
></SPAN
> to a zero of the appropriate type. Subsequent values (including any necessary coercions) are computed as if by the <A HREF="26_1_Glossary.html#function"
><EM CLASS="term"
>function</EM
></A
> <A HREF="f__plus.html" CLASS="funref"
><B
>+</B
></A
>. If <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>into</SPAN
></SPAN
> <VAR CLASS="param"
>var</VAR
> is used, a <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> can be supplied for <VAR CLASS="param"
>var</VAR
> with the <VAR CLASS="param"
>type-spec</VAR
> argument; the consequences are unspecified if a nonnumeric <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> is supplied. If there is no <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>into</SPAN
></SPAN
> variable, the optional <VAR CLASS="param"
>type-spec</VAR
> argument applies to the internal variable that is keeping the sum. The default <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> is <A HREF="26_1_Glossary.html#implementation-dependent"
><EM CLASS="term"
>implementation-dependent</EM
></A
>; but it must be a <A HREF="26_1_Glossary.html#supertype"
><EM CLASS="term"
>supertype</EM
></A
> of <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> <A HREF="t_number.html" CLASS="typeref"
><B
>number</B
></A
>. </P
><P CLASS="j"
>If <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>into</SPAN
></SPAN
> is used, the construct does not provide a default return value; however, the variable is available for use in any <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>finally</SPAN
></SPAN
> clause. </P
><P CLASS="j"
>Certain kinds of accumulation clauses can be combined in a <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> if their destination is the same (the result of <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> or an <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>into</SPAN
></SPAN
> <VAR CLASS="param"
>var</VAR
>) because they are considered to accumulate conceptually compatible quantities. In particular, any elements of following sets of accumulation clauses can be mixed with other elements of the same set for the same destination in a <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
>:</P
><UL
><LI
><P CLASS="j"
><SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>collect</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>append</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>nconc</SPAN
></SPAN
></P
></LI
><LI
><P CLASS="j"
><SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>sum</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>count</SPAN
></SPAN
></P
></LI
><LI
><P CLASS="j"
><SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>maximize</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>minimize</SPAN
></SPAN
></P
></LI
></UL
><PRE CLASS="screen"
>;; Collect every name and the kids in one list by using
;; COLLECT and APPEND.
(loop for name in '(fred sue alice joe june)
for kids in '((bob ken) () () (kris sunshine) ())
collect name
append kids)
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (FRED BOB KEN SUE ALICE JOE KRIS SUNSHINE JUNE)</PRE
><P CLASS="j"
>Any two clauses that do not accumulate the same <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> of <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
> can coexist in a <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> only if each clause accumulates its values into a different <A HREF="26_1_Glossary.html#variable"
><EM CLASS="term"
>variable</EM
></A
>.</P
><H4 ID="sec_6_1_3_1" CLASS="HeadIV"
>6.1.3.1 Examples of COLLECT clause</H4
><PRE CLASS="screen"
>;; Collect all the symbols in a list.
(loop for i in '(bird 3 4 turtle (1 . 4) horse cat)
when (symbolp i) collect i)
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (BIRD TURTLE HORSE CAT)
;; Collect and return odd numbers.
(loop for i from 1 to 10
if (oddp i) collect i)
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (1 3 5 7 9)
;; Collect items into local variable, but don't return them.
(loop for i in '(a b c d) by #'cddr
collect i into my-list
finally (print my-list))
<SPAN CLASS="cmr"
>&#8883;</SPAN
> (A C)
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> NIL</PRE
><H4 ID="sec_6_1_3_2" CLASS="HeadIV"
>6.1.3.2 Examples of APPEND and NCONC clauses</H4
><PRE CLASS="screen"
>;; Use APPEND to concatenate some sublists.
(loop for x in '((a) (b) ((c)))
append x)
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (A B (C))
;; NCONC some sublists together. Note that only lists made by the
;; call to LIST are modified.
(loop for i upfrom 0
as x in '(a b (c))
nconc (if (evenp i) (list x) nil))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (A (C))</PRE
><H4 ID="sec_6_1_3_3" CLASS="HeadIV"
>6.1.3.3 Examples of COUNT clause</H4
><PRE CLASS="screen"
> (loop for i in '(a b nil c nil d e)
count i)
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 5</PRE
><H4 ID="sec_6_1_3_4" CLASS="HeadIV"
>6.1.3.4 Examples of MAXIMIZE and MINIMIZE clauses</H4
><PRE CLASS="screen"
> (loop for i in '(2 1 5 3 4)
maximize i)
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 5
(loop for i in '(2 1 5 3 4)
minimize i)
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 1
;; In this example, FIXNUM applies to the internal variable that holds
;; the maximum value.
(setq series '(1.2 4.3 5.7))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (1.2 4.3 5.7)
(loop for v in series
maximize (round v) of-type fixnum)
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 6
;; In this example, FIXNUM applies to the variable RESULT.
(loop for v of-type float in series
minimize (round v) into result of-type fixnum
finally (return result))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 1</PRE
><H4 ID="sec_6_1_3_5" CLASS="HeadIV"
>6.1.3.5 Examples of SUM clause</H4
><PRE CLASS="screen"
> (loop for i of-type fixnum in '(1 2 3 4 5)
sum i)
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 15
(setq series '(1.2 4.3 5.7))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (1.2 4.3 5.7)
(loop for v in series
sum (* 2.0 v))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 22.4</PRE
><H3 ID="sec_6_1_4" CLASS="HeadIII"
>6.1.4 Termination Test Clauses</H3
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>repeat</SPAN
></SPAN
> construct causes iteration to terminate after a specified number of times. The loop body executes <I CLASS="i"
><I
>n</I
></I
> times, where <I CLASS="i"
><I
>n</I
></I
> is the value of the expression <VAR CLASS="param"
>form</VAR
>. The <VAR CLASS="param"
>form</VAR
> argument is evaluated one time in the loop prologue. If the expression evaluates to 0 or to a negative <A HREF="26_1_Glossary.html#number"
><EM CLASS="term"
>number</EM
></A
>, the loop body is not evaluated. </P
><P CLASS="j"
>The constructs <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>always</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>never</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>thereis</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>while</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>until</SPAN
></SPAN
>, and the macro <A HREF="f_loop-finish.html" CLASS="macref"
><B
>loop-finish</B
></A
> allow conditional termination of iteration within a <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
>. </P
><P CLASS="j"
>The constructs <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>always</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>never</SPAN
></SPAN
>, and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>thereis</SPAN
></SPAN
> provide specific values to be returned when a <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> terminates. Using <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>always</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>never</SPAN
></SPAN
>, or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>thereis</SPAN
></SPAN
> in a loop with value accumulation clauses that are not <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>into</SPAN
></SPAN
> causes an error of <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> <A HREF="t_program-error.html" CLASS="typeref"
><B
>program-error</B
></A
> to be signaled (at macro expansion time). Since <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>always</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>never</SPAN
></SPAN
>, and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>thereis</SPAN
></SPAN
> use the <A HREF="f_return-from.html" CLASS="specref"
><B
>return-from</B
></A
> <A HREF="26_1_Glossary.html#special_operator"
><EM CLASS="term"
>special operator</EM
></A
> to terminate iteration, any <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>finally</SPAN
></SPAN
> clause that is supplied is not evaluated when exit occurs due to any of these constructs. In all other respects these constructs behave like the <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>while</SPAN
></SPAN
> and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>until</SPAN
></SPAN
> constructs. </P
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>always</SPAN
></SPAN
> construct takes one <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> and terminates the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> if the <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> ever evaluates to <SPAN CLASS="misc"
><B
>nil</B
></SPAN
>; in this case, it returns <SPAN CLASS="misc"
><B
>nil</B
></SPAN
>. Otherwise, it provides a default return value of <SPAN CLASS="misc"
><B
>t</B
></SPAN
>. If the value of the supplied <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> is never <SPAN CLASS="misc"
><B
>nil</B
></SPAN
>, some other construct can terminate the iteration. </P
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>never</SPAN
></SPAN
> construct terminates iteration the first time that the value of the supplied <VAR CLASS="param"
>form</VAR
> is <A HREF="26_1_Glossary.html#non-nil"
><EM CLASS="term"
>non-nil</EM
></A
>; the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> returns <SPAN CLASS="misc"
><B
>nil</B
></SPAN
>. If the value of the supplied <VAR CLASS="param"
>form</VAR
> is always <SPAN CLASS="misc"
><B
>nil</B
></SPAN
>, some other construct can terminate the iteration. Unless some other clause contributes a return value, the default value returned is <SPAN CLASS="misc"
><B
>t</B
></SPAN
>. </P
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>thereis</SPAN
></SPAN
> construct terminates iteration the first time that the value of the supplied <VAR CLASS="param"
>form</VAR
> is <A HREF="26_1_Glossary.html#non-nil"
><EM CLASS="term"
>non-nil</EM
></A
>; the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> returns the value of the supplied <VAR CLASS="param"
>form</VAR
>. If the value of the supplied <VAR CLASS="param"
>form</VAR
> is always <SPAN CLASS="misc"
><B
>nil</B
></SPAN
>, some other construct can terminate the iteration. Unless some other clause contributes a return value, the default value returned is <SPAN CLASS="misc"
><B
>nil</B
></SPAN
>. </P
><P CLASS="j"
>There are two differences between the <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>thereis</SPAN
></SPAN
> and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>until</SPAN
></SPAN
> constructs:</P
><UL
><LI
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>until</SPAN
></SPAN
> construct does not return a value or <SPAN CLASS="misc"
><B
>nil</B
></SPAN
> based on the value of the supplied <VAR CLASS="param"
>form</VAR
>.</P
></LI
><LI
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>until</SPAN
></SPAN
> construct executes any <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>finally</SPAN
></SPAN
> clause. Since <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>thereis</SPAN
></SPAN
> uses the <A HREF="f_return-from.html" CLASS="specref"
><B
>return-from</B
></A
> <A HREF="26_1_Glossary.html#special_operator"
><EM CLASS="term"
>special operator</EM
></A
> to terminate iteration, any <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>finally</SPAN
></SPAN
> clause that is supplied is not evaluated when exit occurs due to <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>thereis</SPAN
></SPAN
>.</P
></LI
></UL
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>while</SPAN
></SPAN
> construct allows iteration to continue until the supplied <VAR CLASS="param"
>form</VAR
> evaluates to <A HREF="26_1_Glossary.html#false"
><EM CLASS="term"
>false</EM
></A
>. The supplied <VAR CLASS="param"
>form</VAR
> is reevaluated at the location of the <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>while</SPAN
></SPAN
> clause. </P
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>until</SPAN
></SPAN
> construct is equivalent to <CODE CLASS="f"
>while (not <VAR CLASS="param"
>form</VAR
>)<I
>.</I
>&#8201;<I
>.</I
>&#8201;<I
>.</I
></CODE
>&#8201;. If the value of the supplied <VAR CLASS="param"
>form</VAR
> is <A HREF="26_1_Glossary.html#non-nil"
><EM CLASS="term"
>non-nil</EM
></A
>, iteration terminates. </P
><P CLASS="j"
>Termination-test control constructs can be used anywhere within the loop body. The termination tests are used in the order in which they appear. If an <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>until</SPAN
></SPAN
> or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>while</SPAN
></SPAN
> clause causes termination, any clauses that precede it in the source are still evaluated. If the <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>until</SPAN
></SPAN
> and <SPAN CLASS="cmtt"
>while</SPAN
> constructs cause termination, control is passed to the loop epilogue, where any <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>finally</SPAN
></SPAN
> clauses will be executed. </P
><P CLASS="j"
>There are two differences between the <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>never</SPAN
></SPAN
> and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>until</SPAN
></SPAN
> constructs:</P
><UL
><LI
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>until</SPAN
></SPAN
> construct does not return <SPAN CLASS="misc"
><B
>t</B
></SPAN
> or <SPAN CLASS="misc"
><B
>nil</B
></SPAN
> based on the value of the supplied <VAR CLASS="param"
>form</VAR
>.</P
></LI
><LI
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>until</SPAN
></SPAN
> construct does not bypass any <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>finally</SPAN
></SPAN
> clauses. Since <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>never</SPAN
></SPAN
> uses the <A HREF="f_return-from.html" CLASS="specref"
><B
>return-from</B
></A
> <A HREF="26_1_Glossary.html#special_operator"
><EM CLASS="term"
>special operator</EM
></A
> to terminate iteration, any <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>finally</SPAN
></SPAN
> clause that is supplied is not evaluated when exit occurs due to <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>never</SPAN
></SPAN
>.</P
></LI
></UL
><P CLASS="j"
>In most cases it is not necessary to use <A HREF="f_loop-finish.html" CLASS="macref"
><B
>loop-finish</B
></A
> because other loop control clauses terminate the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
>. The macro <A HREF="f_loop-finish.html" CLASS="macref"
><B
>loop-finish</B
></A
> is used to provide a normal exit from a nested conditional inside a <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
>. Since <A HREF="f_loop-finish.html" CLASS="macref"
><B
>loop-finish</B
></A
> transfers control to the loop epilogue, using <A HREF="f_loop-finish.html" CLASS="macref"
><B
>loop-finish</B
></A
> within a <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>finally</SPAN
></SPAN
> expression can cause infinite looping.</P
><H4 ID="sec_6_1_4_1" CLASS="HeadIV"
>6.1.4.1 Examples of REPEAT clause</H4
><PRE CLASS="screen"
> (loop repeat 3
do (format t "~&amp;What I say three times is true.~%"))
<SPAN CLASS="cmr"
>&#8883;</SPAN
> What I say three times is true.
<SPAN CLASS="cmr"
>&#8883;</SPAN
> What I say three times is true.
<SPAN CLASS="cmr"
>&#8883;</SPAN
> What I say three times is true.
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> NIL
(loop repeat -15
do (format t "What you see is what you expect~%"))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> NIL</PRE
><H4 ID="sec_6_1_4_2" CLASS="HeadIV"
>6.1.4.2 Examples of ALWAYS, NEVER, and THEREIS clauses</H4
><PRE CLASS="screen"
>;; Make sure I is always less than 11 (two ways).
;; The FOR construct terminates these loops.
(loop for i from 0 to 10
always (&lt; i 11))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> T
(loop for i from 0 to 10
never (&gt; i 11))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> T
;; If I exceeds 10 return I; otherwise, return NIL.
;; The THEREIS construct terminates this loop.
(loop for i from 0
thereis (when (&gt; i 10) i) )
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 11
;;; The FINALLY clause is not evaluated in these examples.
(loop for i from 0 to 10
always (&lt; i 9)
finally (print "you won't see this"))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> NIL
(loop never t
finally (print "you won't see this"))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> NIL
(loop thereis "Here is my value"
finally (print "you won't see this"))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> "Here is my value"
;; The FOR construct terminates this loop, so the FINALLY clause
;; is evaluated.
(loop for i from 1 to 10
thereis (&gt; i 11)
finally (prin1 'got-here))
<SPAN CLASS="cmr"
>&#8883;</SPAN
> GOT-HERE
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> NIL
;; If this code could be used to find a counterexample to Fermat's
;; last theorem, it would still not return the value of the
;; counterexample because all of the THEREIS clauses in this example
;; only return T. But if Fermat is right, that won't matter
;; because this won't terminate.
(loop for z upfrom 2
thereis
(loop for n upfrom 3 below (log z 2)
thereis
(loop for x below z
thereis
(loop for y below z
thereis (= (+ (expt x n) (expt y n))
(expt z n))))))</PRE
><H4 ID="sec_6_1_4_3" CLASS="HeadIV"
>6.1.4.3 Examples of WHILE and UNTIL clauses</H4
><PRE CLASS="screen"
> (loop while (hungry-p) do (eat))
;; UNTIL NOT is equivalent to WHILE.
(loop until (not (hungry-p)) do (eat))
;; Collect the length and the items of STACK.
(let ((stack '(a b c d e f)))
(loop for item = (length stack) then (pop stack)
collect item
while stack))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (6 A B C D E F)
;; Use WHILE to terminate a loop that otherwise wouldn't terminate.
;; Note that WHILE occurs after the WHEN.
(loop for i fixnum from 3
when (oddp i) collect i
while (&lt; i 5))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (3 5)</PRE
><H3 ID="sec_6_1_5" CLASS="HeadIII"
>6.1.5 Unconditional Execution Clauses</H3
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>do</SPAN
></SPAN
> and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>doing</SPAN
></SPAN
> constructs evaluate the supplied <VAR CLASS="param"
>forms</VAR
> wherever they occur in the expanded form of <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
>. The <VAR CLASS="param"
>form</VAR
> argument can be any <A HREF="26_1_Glossary.html#compound_form"
><EM CLASS="term"
>compound form</EM
></A
>. Each <VAR CLASS="param"
>form</VAR
> is evaluated in every iteration. Because every loop clause must begin with a <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keyword</EM
></A
>, the keyword <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>do</SPAN
></SPAN
> is used when no control action other than execution is required. </P
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>return</SPAN
></SPAN
> construct takes one <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
>. Any <A HREF="26_1_Glossary.html#value"
><EM CLASS="term"
>values</EM
></A
> returned by the <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> are immediately returned by the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> form. It is equivalent to the clause <CODE CLASS="f"
>do (return-from <I CLASS="i"
><I
>block-name</I
></I
> <I CLASS="i"
><I
>value</I
></I
>)</CODE
>, where <I CLASS="i"
><I
>block-name</I
></I
> is the name specified in a <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>named</SPAN
></SPAN
> clause, or <SPAN CLASS="misc"
><B
>nil</B
></SPAN
> if there is no <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>named</SPAN
></SPAN
> clause.</P
><H4 ID="sec_6_1_5_1" CLASS="HeadIV"
>6.1.5.1 Examples of unconditional execution</H4
><PRE CLASS="screen"
>;; Print numbers and their squares.
;; The DO construct applies to multiple forms.
(loop for i from 1 to 3
do (print i)
(print (* i i)))
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 1
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 1
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 2
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 4
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 3
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 9
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> NIL</PRE
><H3 ID="sec_6_1_6" CLASS="HeadIII"
>6.1.6 Conditional Execution Clauses</H3
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>if</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>when</SPAN
></SPAN
>, and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>unless</SPAN
></SPAN
> constructs establish conditional control in a <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
>. If the test passes, the succeeding loop clause is executed. If the test does not pass, the succeeding clause is skipped, and program control moves to the clause that follows the <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keyword</EM
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>else</SPAN
></SPAN
>. If the test does not pass and no <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>else</SPAN
></SPAN
> clause is supplied, control is transferred to the clause or construct following the entire conditional clause. </P
><P CLASS="j"
>If conditional clauses are nested, each <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>else</SPAN
></SPAN
> is paired with the closest preceding conditional clause that has no associated <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>else</SPAN
></SPAN
> or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>end</SPAN
></SPAN
>. </P
><P CLASS="j"
>In the <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>if</SPAN
></SPAN
> and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>when</SPAN
></SPAN
> clauses, which are synonymous, the test passes if the value of <VAR CLASS="param"
>form</VAR
> is <A HREF="26_1_Glossary.html#true"
><EM CLASS="term"
>true</EM
></A
>. </P
><P CLASS="j"
>In the <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>unless</SPAN
></SPAN
> clause, the test passes if the value of <VAR CLASS="param"
>form</VAR
> is <A HREF="26_1_Glossary.html#false"
><EM CLASS="term"
>false</EM
></A
>. </P
><P CLASS="j"
>Clauses that follow the test expression can be grouped by using the <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keyword</EM
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>and</SPAN
></SPAN
> to produce a conditional block consisting of a compound clause. </P
><P CLASS="j"
>The <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keyword</EM
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>it</SPAN
></SPAN
> can be used to refer to the result of the test expression in a clause. Use the <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keyword</EM
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>it</SPAN
></SPAN
> in place of the form in a <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>return</SPAN
></SPAN
> clause or an <I CLASS="i"
><I
>accumulation</I
></I
> clause that is inside a conditional execution clause. If multiple clauses are connected with <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>and</SPAN
></SPAN
>, the <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>it</SPAN
></SPAN
> construct must be in the first clause in the block. </P
><P CLASS="j"
>The optional <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keyword</EM
></A
> <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>end</SPAN
></SPAN
> marks the end of the clause. If this keyword is not supplied, the next <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keyword</EM
></A
> marks the end. The construct <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>end</SPAN
></SPAN
> can be used to distinguish the scoping of compound clauses.</P
><H4 ID="sec_6_1_6_1" CLASS="HeadIV"
>6.1.6.1 Examples of WHEN clause</H4
><PRE CLASS="screen"
>;; Signal an exceptional condition.
(loop for item in '(1 2 3 a 4 5)
when (not (numberp item))
return (cerror "enter new value" "non-numeric value: ~s" item))
Error: non-numeric value: A
;; The previous example is equivalent to the following one.
(loop for item in '(1 2 3 a 4 5)
when (not (numberp item))
do (return
(cerror "Enter new value" "non-numeric value: ~s" item)))
Error: non-numeric value: A</PRE
><PRE CLASS="screen"
>;; This example parses a simple printed string representation from
;; BUFFER (which is itself a string) and returns the index of the
;; closing double-quote character.
(let ((buffer "\"a\" \"b\""))
(loop initially (unless (char= (char buffer 0) #\")
(loop-finish))
for i of-type fixnum from 1 below (length (the string buffer))
when (char= (char buffer i) #\")
return i))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 2
;; The collected value is returned.
(loop for i from 1 to 10
when (&gt; i 5)
collect i
finally (prin1 'got-here))
<SPAN CLASS="cmr"
>&#8883;</SPAN
> GOT-HERE
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (6 7 8 9 10)
;; Return both the count of collected numbers and the numbers.
(loop for i from 1 to 10
when (&gt; i 5)
collect i into number-list
and count i into number-count
finally (return (values number-count number-list)))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 5, (6 7 8 9 10)</PRE
><H3 ID="sec_6_1_7" CLASS="HeadIII"
>6.1.7 Miscellaneous Clauses</H3
><H4 ID="sec_6_1_7_1" CLASS="HeadIV"
>6.1.7.1 Control Transfer Clauses</H4
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>named</SPAN
></SPAN
> construct establishes a name for an <A HREF="26_1_Glossary.html#implicit_block"
><EM CLASS="term"
>implicit block</EM
></A
> surrounding the entire <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> so that the <A HREF="f_return-from.html" CLASS="specref"
><B
>return-from</B
></A
> <A HREF="26_1_Glossary.html#special_operator"
><EM CLASS="term"
>special operator</EM
></A
> can be used to return values from or to exit <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
>. Only one name per <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> can be assigned. If used, the <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>named</SPAN
></SPAN
> construct must be the first clause in the loop expression. </P
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>return</SPAN
></SPAN
> construct takes one <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
>. Any <A HREF="26_1_Glossary.html#value"
><EM CLASS="term"
>values</EM
></A
> returned by the <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> are immediately returned by the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> form. This construct is similar to the <A HREF="f_return-from.html" CLASS="specref"
><B
>return-from</B
></A
> <A HREF="26_1_Glossary.html#special_operator"
><EM CLASS="term"
>special operator</EM
></A
> and the <A HREF="f_return.html" CLASS="macref"
><B
>return</B
></A
> <A HREF="26_1_Glossary.html#macro"
><EM CLASS="term"
>macro</EM
></A
>. The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>return</SPAN
></SPAN
> construct does not execute any <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>finally</SPAN
></SPAN
> clause that the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> is given.</P
><H5 ID="sec_6_1_7_1_1" CLASS="HeadV"
>6.1.7.1.1 Examples of NAMED clause</H5
><PRE CLASS="screen"
>;; Just name and return.
(loop named max
for i from 1 to 10
do (print i)
do (return-from max 'done))
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 1
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> DONE</PRE
><H4 ID="sec_6_1_7_2" CLASS="HeadIV"
>6.1.7.2 Initial and Final Execution</H4
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>initially</SPAN
></SPAN
> and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>finally</SPAN
></SPAN
> constructs evaluate forms that occur before and after the loop body. </P
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>initially</SPAN
></SPAN
> construct causes the supplied <VAR CLASS="param"
>compound-forms</VAR
> to be evaluated in the loop prologue, which precedes all loop code except for initial settings supplied by constructs <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>with</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>for</SPAN
></SPAN
>, or <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>as</SPAN
></SPAN
>. The code for any <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>initially</SPAN
></SPAN
> clauses is executed in the order in which the clauses appeared in the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
>. </P
><P CLASS="j"
>The <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>finally</SPAN
></SPAN
> construct causes the supplied <VAR CLASS="param"
>compound-forms</VAR
> to be evaluated in the loop epilogue after normal iteration terminates. The code for any <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>finally</SPAN
></SPAN
> clauses is executed in the order in which the clauses appeared in the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
>. The collected code is executed once in the loop epilogue before any implicit values are returned from the accumulation clauses. An explicit transfer of control (<I CLASS="i"
><I
>e.g.</I
></I
>, by <A HREF="f_return.html" CLASS="macref"
><B
>return</B
></A
>, <A HREF="f_go.html" CLASS="specref"
><B
>go</B
></A
>, or <A HREF="f_throw.html" CLASS="specref"
><B
>throw</B
></A
>) from the loop body, however, will exit the <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> without executing the epilogue code. </P
><P CLASS="j"
>Clauses such as <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>return</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>always</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>never</SPAN
></SPAN
>, and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>thereis</SPAN
></SPAN
> can bypass the <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>finally</SPAN
></SPAN
> clause. <A HREF="f_return.html" CLASS="macref"
><B
>return</B
></A
> (or <A HREF="f_return-from.html" CLASS="specref"
><B
>return-from</B
></A
>, if the <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>named</SPAN
></SPAN
> option was supplied) can be used after <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>finally</SPAN
></SPAN
> to return values from a <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
>. Such an <A HREF="26_1_Glossary.html#explicit_return"
><EM CLASS="term"
>explicit return</EM
></A
> inside the <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>finally</SPAN
></SPAN
> clause takes precedence over returning the accumulation from clauses supplied by such keywords as <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>collect</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>nconc</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>append</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>sum</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>count</SPAN
></SPAN
>, <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>maximize</SPAN
></SPAN
>, and <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>minimize</SPAN
></SPAN
>; the accumulation values for these preempted clauses are not returned by <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> if <A HREF="f_return.html" CLASS="macref"
><B
>return</B
></A
> or <A HREF="f_return-from.html" CLASS="specref"
><B
>return-from</B
></A
> is used.</P
><H3 ID="sec_6_1_8" CLASS="HeadIII"
>6.1.8 Examples of Miscellaneous Loop Features</H3
><PRE CLASS="screen"
>(let ((i 0)) ; no loop keywords are used
(loop (incf i) (if (= i 3) (return i)))) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 3
(let ((i 0)(j 0))
(tagbody
(loop (incf j 3) (incf i) (if (= i 3) (go exit)))
exit)
j) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 9</PRE
><P CLASS="j"
>In the following example, the variable <CODE CLASS="f"
>x</CODE
> is stepped before <CODE CLASS="f"
>y</CODE
> is stepped; thus, the value of <CODE CLASS="f"
>y</CODE
> reflects the updated value of <CODE CLASS="f"
>x</CODE
>:</P
><PRE CLASS="screen"
> (loop for x from 1 to 10
for y = nil then x
collect (list x y))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> ((1 NIL) (2 2) (3 3) (4 4) (5 5) (6 6) (7 7) (8 8) (9 9) (10 10))</PRE
><P CLASS="j"
>In this example, <CODE CLASS="f"
>x</CODE
> and <CODE CLASS="f"
>y</CODE
> are stepped in <A HREF="26_1_Glossary.html#parallel"
><EM CLASS="term"
>parallel</EM
></A
>:</P
><PRE CLASS="screen"
> (loop for x from 1 to 10
and y = nil then x
collect (list x y))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> ((1 NIL) (2 1) (3 2) (4 3) (5 4) (6 5) (7 6) (8 7) (9 8) (10 9))</PRE
><H4 ID="sec_6_1_8_1" CLASS="HeadIV"
>6.1.8.1 Examples of clause grouping</H4
><PRE CLASS="screen"
>;; Group conditional clauses.
(loop for i in '(1 324 2345 323 2 4 235 252)
when (oddp i)
do (print i)
and collect i into odd-numbers
and do (terpri)
else ; I is even.
collect i into even-numbers
finally
(return (values odd-numbers even-numbers)))
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 1
<SPAN CLASS="cmr"
>&#8883;</SPAN
>
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 2345
<SPAN CLASS="cmr"
>&#8883;</SPAN
>
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 323
<SPAN CLASS="cmr"
>&#8883;</SPAN
>
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 235
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (1 2345 323 235), (324 2 4 252)
;; Collect numbers larger than 3.
(loop for i in '(1 2 3 4 5 6)
when (and (&gt; i 3) i)
collect it) ; IT refers to (and (&gt; i 3) i).
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (4 5 6)
;; Find a number in a list.
(loop for i in '(1 2 3 4 5 6)
when (and (&gt; i 3) i)
return it)
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 4
;; The above example is similar to the following one.
(loop for i in '(1 2 3 4 5 6)
thereis (and (&gt; i 3) i))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 4
;; Nest conditional clauses.
(let ((list '(0 3.0 apple 4 5 9.8 orange banana)))
(loop for i in list
when (numberp i)
when (floatp i)
collect i into float-numbers
else ; Not (floatp i)
collect i into other-numbers
else ; Not (numberp i)
when (symbolp i)
collect i into symbol-list
else ; Not (symbolp i)
do (error "found a funny value in list ~S, value ~S~%" list i)
finally (return (values float-numbers other-numbers symbol-list))))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (3.0 9.8), (0 4 5), (APPLE ORANGE BANANA)
;; Without the END preposition, the last AND would apply to the
;; inner IF rather than the outer one.
(loop for x from 0 to 3
do (print x)
if (zerop (mod x 2))
do (princ " a")
and if (zerop (floor x 2))
do (princ " b")
end
and do (princ " c"))
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 0 a b c
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 1
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 2 a c
<SPAN CLASS="cmr"
>&#8883;</SPAN
> 3
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> NIL</PRE
><H3 ID="sec_6_1_9" CLASS="HeadIII"
>6.1.9 Notes about Loop</H3
><P CLASS="j"
><A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>Types</EM
></A
> can be supplied for loop variables. It is not necessary to supply a <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> for any variable, but supplying the <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> can ensure that the variable has a correctly typed initial value, and it can also enable compiler optimizations (depending on the <A HREF="26_1_Glossary.html#implementation"
><EM CLASS="term"
>implementation</EM
></A
>). </P
><P CLASS="j"
>The clause <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>repeat</SPAN
></SPAN
> <I CLASS="i"
><I
>n</I
></I
> ... is roughly equivalent to a clause such as</P
><PRE CLASS="screen"
>(loop for <I CLASS="i"
><I
>internal-variable</I
></I
> downfrom (- <I CLASS="i"
><I
>n</I
></I
> 1) to 0 ...)</PRE
><P CLASS="j"
>but in some <A HREF="26_1_Glossary.html#implementation"
><EM CLASS="term"
>implementations</EM
></A
>, the <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>repeat</SPAN
></SPAN
> construct might be more efficient. </P
><P CLASS="j"
>Within the executable parts of the loop clauses and around the entire <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
> form, variables can be bound by using <A HREF="f_let.html" CLASS="specref"
><B
>let</B
></A
>. </P
><P CLASS="j"
>Use caution when using a variable named <SPAN CLASS="cmtt"
>IT</SPAN
> (in any <A HREF="26_1_Glossary.html#package"
><EM CLASS="term"
>package</EM
></A
>) in connection with <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
>, since <SPAN CLASS="loopref"
><SPAN CLASS="cmtt"
>it</SPAN
></SPAN
> is a <A HREF="26_1_Glossary.html#loop_keyword"
><EM CLASS="term"
>loop keyword</EM
></A
> that can be used in place of a <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> in certain contexts. </P
><P CLASS="j"
>There is no <A HREF="26_1_Glossary.html#standardized"
><EM CLASS="term"
>standardized</EM
></A
> mechanism for users to add extensions to <A HREF="f_loop.html" CLASS="macref"
><B
>loop</B
></A
>.</P
></SECTION
></DIV
><DIV CLASS="footer"
><DIV CLASS="btmnav"
><A HREF="6_Iteration.html" CLASS="prev"
>&#8592;</A
><A HREF="6_2_Iteration_Dictionary.html" CLASS="next"
>&#8594;</A
></DIV
><DIV CLASS="trail"
>Conversion to HTML copyright 2023 by Gilbert Baumann</DIV
></DIV
></DIV
><SCRIPT
>domReady();</SCRIPT
></BODY
></HTML
>