60 lines
7.5 KiB
HTML
60 lines
7.5 KiB
HTML
|
<!-- Common Lisp HyperSpec (TM), version 7.0 generated by Kent M. Pitman on Mon, 11-Apr-2005 2:31am EDT -->
|
||
|
<HTML>
|
||
|
<HEAD>
|
||
|
<TITLE>CLHS: Section 6.1.2.1.1</TITLE>
|
||
|
<LINK HREF="../Data/clhs.css" REL="stylesheet" TYPE="text/css" />
|
||
|
<META HTTP-EQUIV="Author" CONTENT="Kent M. Pitman">
|
||
|
<META HTTP-EQUIV="Organization" CONTENT="LispWorks Ltd.">
|
||
|
<LINK REL=TOP HREF="../Front/index.htm">
|
||
|
<LINK REL=COPYRIGHT HREF="../Front/Help.htm#Legal">
|
||
|
<LINK REL=DISCLAIMER HREF="../Front/Help.htm#Disclaimer">
|
||
|
<LINK REL=PREV HREF="06_aba.htm">
|
||
|
<LINK REL=UP HREF="06_aba.htm">
|
||
|
<LINK REL=NEXT HREF="06_abaaa.htm">
|
||
|
</HEAD>
|
||
|
<BODY>
|
||
|
<H1><A REV=MADE HREF="http://www.lispworks.com/"><IMG WIDTH=80 HEIGHT=65 ALT="[LISPWORKS]" SRC="../Graphics/LWSmall.gif" ALIGN=Bottom></A><A REL=TOP HREF="../Front/index.htm"><IMG WIDTH=237 HEIGHT=65 ALT="[Common Lisp HyperSpec (TM)]" SRC="../Graphics/CLHS_Sm.gif" ALIGN=Bottom></A> <A REL=PREV HREF="06_aba.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="06_aba.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="06_abaaa.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
|
||
|
|
||
|
<HR>
|
||
|
|
||
|
<H2>
|
||
|
6.1.2.1.1 The for-as-arithmetic subclause</H2> <P>
|
||
|
In the <I>for-as-arithmetic</I> subclause, the <TT>for</TT> or <TT>as</TT> construct iterates from the value supplied by <I>form1</I> to the value supplied by <I>form2</I> in increments or decrements denoted by <I>form3</I>. Each expression is evaluated only once and must evaluate to a <A REL=DEFINITION HREF="26_glo_n.htm#number"><I>number</I></A>. The variable <I>var</I> is bound to the value of <I>form1</I> in the first iteration and is <I>stepped</I>[1] by the value of <I>form3</I> in each succeeding iteration, or by 1 if <I>form3</I> is not provided. The following <A REL=DEFINITION HREF="26_glo_l.htm#loop_keyword"><I>loop keywords</I></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>
|
||
|
<P><DL><P>
|
||
|
<DT><TT>from </TT>|<TT> downfrom </TT>|<TT> upfrom</TT> <P><DD>
|
||
|
<DT><TT>to </TT>|<TT> downto </TT>|<TT> upto </TT>|<TT> below </TT>|<TT> above</TT> <P><DD>
|
||
|
<DT><TT>by</TT> <P><DD></DL><P>
|
||
|
The prepositional phrases in each subclause may appear in any order. For example, either ``<TT>from x by y</TT>'' or ``<TT>by y from x</TT>'' is permitted. However, because left-to-right order of evaluation is preserved, the effects will be different in the case of side effects. Consider: <P>
|
||
|
<PRE>
|
||
|
(let ((x 1)) (loop for i from x by (incf x) to 10 collect i))
|
||
|
=> (1 3 5 7 9)
|
||
|
(let ((x 1)) (loop for i by (incf x) from x to 10 collect i))
|
||
|
=> (2 4 6 8 10)
|
||
|
</PRE>
|
||
|
</TT> <P>
|
||
|
The descriptions of the prepositions follow: <P>
|
||
|
<P><DL><DT><TT>from</TT> <P><DD>
|
||
|
The <A REL=DEFINITION HREF="26_glo_l.htm#loop_keyword"><I>loop keyword</I></A> <TT>from</TT> specifies the value from which <I>stepping</I>[1] begins, as supplied by <I>form1</I>. <I>Stepping</I>[1] is incremental by default. If decremental <I>stepping</I>[1] is desired, the preposition <TT>downto</TT> or <TT>above</TT> must be used with <I>form2</I>. For incremental <I>stepping</I>[1], the default <TT>from</TT> value is 0. <P>
|
||
|
<DT><TT>downfrom, upfrom</TT> <P><DD>
|
||
|
The <A REL=DEFINITION HREF="26_glo_l.htm#loop_keyword"><I>loop keyword</I></A> <TT>downfrom</TT> indicates that the variable <I>var</I> is decreased in decrements supplied by <I>form3</I>; the <A REL=DEFINITION HREF="26_glo_l.htm#loop_keyword"><I>loop keyword</I></A> <TT>upfrom</TT> indicates that <I>var</I> is increased in increments supplied by <I>form3</I>. <P>
|
||
|
<DT><TT>to</TT> <P><DD>
|
||
|
The <A REL=DEFINITION HREF="26_glo_l.htm#loop_keyword"><I>loop keyword</I></A> <TT>to</TT> marks the end value for <I>stepping</I>[1] supplied in <I>form2</I>. <I>Stepping</I>[1] is incremental by default. If decremental <I>stepping</I>[1] is desired, the preposition <TT>downfrom</TT> must be used with <I>form1</I>, or else the preposition <TT>downto</TT> or <TT>above</TT> should be used instead of <TT>to</TT> with <I>form2</I>. <P>
|
||
|
<DT><TT>downto, upto</TT> <P><DD>
|
||
|
The <A REL=DEFINITION HREF="26_glo_l.htm#loop_keyword"><I>loop keyword</I></A> <TT>downto</TT> specifies decremental <I>stepping</I>; the <A REL=DEFINITION HREF="26_glo_l.htm#loop_keyword"><I>loop keyword</I></A> <TT>upto</TT> specifies incremental <I>stepping</I>. In both cases, the amount of change on each step is specified by <I>form3</I>, and the <A REL=DEFINITION HREF="m_loop.htm#loop"><B>loop</B></A> terminates when the variable <I>var</I> passes the value of <I>form2</I>. Since there is no default for <I>form1</I> in decremental <I>stepping</I>[1], a <I>form1</I> value must be supplied (using <TT>from</TT> or <TT>downfrom</TT>) when <TT>downto</TT> is supplied. <P>
|
||
|
<DT><TT>below, above</TT> <P><DD>
|
||
|
The <A REL=DEFINITION HREF="26_glo_l.htm#loop_keyword"><I>loop keywords</I></A> <TT>below</TT> and <TT>above</TT> are analogous to <TT>upto</TT> and <TT>downto</TT> respectively. These keywords stop iteration just before the value of the variable <I>var</I> reaches the value supplied by <I>form2</I>; the end value of <I>form2</I> is not included. Since there is no default for <I>form1</I> in decremental <I>stepping</I>[1], a <I>form1</I> value must be supplied (using <TT>from</TT> or <TT>downfrom</TT>) when <TT>above</TT> is supplied. <P>
|
||
|
<DT><TT>by</TT> <P><DD>
|
||
|
The <A REL=DEFINITION HREF="26_glo_l.htm#loop_keyword"><I>loop keyword</I></A> <TT>by</TT> marks the increment or decrement supplied by <I>form3</I>. The value of <I>form3</I> can be any positive <A REL=DEFINITION HREF="26_glo_n.htm#number"><I>number</I></A>. The default value is 1. <P>
|
||
|
<P></DL><P>
|
||
|
In an iteration control clause, the <TT>for</TT> or <TT>as</TT> construct causes termination when the supplied limit is reached. That is, iteration continues until the value <I>var</I> is stepped to the exclusive or inclusive limit supplied by <I>form2</I>. The range is exclusive if <I>form3</I> increases or decreases <I>var</I> to the value of <I>form2</I> without reaching that value; the loop keywords <TT>below</TT> and <TT>above</TT> provide exclusive limits. An inclusive limit allows <I>var</I> to attain the value of <I>form2</I>; <TT>to</TT>, <TT>downto</TT>, and <TT>upto</TT> provide inclusive limits. <P>
|
||
|
|
||
|
|
||
|
<A REL=CHILD HREF="06_abaaa.htm"><H2>
|
||
|
6.1.2.1.1.1 Examples of for-as-arithmetic subclause</H2></A><P><P><HR>The following <A REL=META HREF="../Front/X3J13Iss.htm">X3J13 cleanup issue</A>, <I>not part of the specification</I>, applies to this section:<P><UL><LI> <A REL=CHILD HREF="../Issues/iss223.htm">LOOP-MISCELLANEOUS-REPAIRS:FIX</A><P></UL><HR>
|
||
|
|
||
|
<A REL=NAVIGATOR HREF="../Front/StartPts.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Starting Points]" SRC="../Graphics/StartPts.gif" ALIGN=Bottom></A><A REL=TOC HREF="../Front/Contents.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Contents]" SRC="../Graphics/Contents.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Master.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Index]" SRC="../Graphics/Index.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Symbol.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Symbols]" SRC="../Graphics/Symbols.gif" ALIGN=Bottom></A><A REL=GLOSSARY HREF="../Body/26_a.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Glossary]" SRC="../Graphics/Glossary.gif" ALIGN=Bottom></A><A HREF="../Front/X3J13Iss.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Issues]" SRC="../Graphics/Issues.gif" ALIGN=Bottom></A><BR>
|
||
|
|
||
|
<A REL=COPYRIGHT HREF="../Front/Help.htm#Legal"><I>Copyright 1996-2005, LispWorks Ltd. All rights reserved.</I></A><P>
|
||
|
</BODY>
|
||
|
</HTML>
|