emacs.d/clones/lisp/www.cs.cmu.edu/Groups/AI/html/cltl/clm/node240.html

191 lines
8.3 KiB
HTML
Raw Normal View History

2022-08-26 19:11:35 +02:00
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
<!Converted with LaTeX2HTML 0.6.5 (Tue Nov 15 1994) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
<HEAD>
<TITLE>26.3.2. Kinds of Loop Clauses</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Kinds of Loop Clauses">
<meta name="keywords" value="clm">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<b>Common Lisp the Language, 2nd Edition</b>
<BR> <HR><A NAME=tex2html4570 HREF="node241.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html4568 HREF="node238.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html4562 HREF="node239.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html4572 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html4573 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html4571 HREF="node241.html"> Loop Syntax</A>
<B>Up:</B> <A NAME=tex2html4569 HREF="node238.html"> Parsing Loop Clauses</A>
<B> Previous:</B> <A NAME=tex2html4563 HREF="node239.html"> Order of Execution</A>
<HR> <P>
<H2><A NAME=SECTION003032000000000000000>26.3.2. Kinds of Loop Clauses</A></H2>
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
<A NAME=LOOPKINDSSECTION>Loop</A>
clauses fall into one of the following categories:
<P>
<UL> <LI>
variable initialization and stepping
<P>
<UL> <LI>
The <tt>for</tt> and <tt>as</tt> constructs provide iteration control clauses
that establish a variable to be initialized.
You can combine <tt>for</tt> and <tt>as</tt> clauses with the loop
keyword <tt>and</tt> to get parallel initialization and stepping.
<P>
<LI>
The <tt>with</tt> construct is similar to a single <tt>let</tt> clause.
You can combine <tt>with</tt> clauses using
<tt>and</tt> to get parallel initialization.
<P>
<LI>
The <tt>repeat</tt> construct causes iteration to terminate after a specified
number of times. It uses an internal variable to keep track of the
number of iterations.
</UL>
<P>
You can specify data types for loop variables (see
section <A HREF="node251.html#LOOPTYPESSECTION">26.12.1</A>).
It is an error to bind the same variable twice in any variable-binding
clause of a single loop expression. Such variables include
local variables, iteration control variables, and variables found by
destructuring.
<P>
<LI> value accumulation
<P>
<UL> <LI> The <tt>collect</tt> construct takes one form in its clause
and adds the value of that form to the end of a list of values. By
default, the list of values is returned when the loop finishes.
<P>
<LI>
The <tt>append</tt> construct takes one form in its clause
and appends the value of that form to the end of a list of values. By
default, the list of values is returned when the loop finishes.
<P>
<LI> The <tt>nconc</tt> construct is similar to <tt>append</tt>, but
its list values are concatenated as if by the Common Lisp function
<tt>nconc</tt>. By
default, the list of values is returned when the loop finishes.
<P>
<LI> The <tt>sum</tt> construct takes one form in its clause that
must evaluate to a number and adds that number into a running total.
By default, the cumulative sum is returned when the loop finishes.
<P>
<LI>
The <tt>count</tt> construct takes one form in its clause and counts the
number of times that the form evaluates to a non-<tt>nil</tt> value. By
default, the count is returned when the loop finishes.
<P>
<LI>
The <tt>minimize</tt> construct takes one form in its clause and determines
the minimum value obtained by evaluating that form. By default, the
minimum value is returned when the loop finishes.
<P>
<LI>
The <tt>maximize</tt> construct takes one form in its clause and
determines the maximum value obtained by evaluating that form. By
default, the maximum value is returned when the loop finishes.
</UL>
<P>
<LI>
termination conditions
<P>
<UL> <LI>
The <tt>loop-finish</tt> Lisp macro terminates iteration and returns any
accumulated result. If specified, any <tt>finally</tt> clauses are evaluated.
<P>
<LI>
The <tt>for</tt> and <tt>as</tt> constructs provide a termination test
that is determined by the iteration control clause.
<P>
<LI>
The <tt>repeat</tt> construct causes termination after a specified
number of iterations.
<P>
<LI>
The <tt>while</tt> construct takes one form, a condition, and terminates
the iteration if
the condition evaluates to <tt>nil</tt>. A <tt>while</tt> clause is
equivalent to the expression <tt>(if (not <i>condition</i>) (loop-finish))</tt>.
<P>
<LI>
The <tt>until</tt> construct is the inverse of <tt>while</tt>;
it terminates the iteration if the condition evaluates to any non-<tt>nil</tt>
value. An <tt>until</tt> clause is equivalent to the expression
<tt>(if <i>condition</i> (loop-finish))</tt>.
<P>
<LI>
The <tt>always</tt> construct takes one form and terminates the loop
if the form ever evaluates to <tt>nil</tt>; in this case, it returns
<tt>nil</tt>. Otherwise, it provides a default return value of <tt>t</tt>.
<P>
<LI>
The <tt>never</tt> construct takes one form and terminates the loop
if the form ever evaluates to non-<tt>nil</tt>; in this case, it returns
<tt>nil</tt>. Otherwise, it provides a default return value of <tt>t</tt>.
<P>
<LI>
The <tt>thereis</tt> construct takes one form and terminates the loop
if the form ever evaluates to non-<tt>nil</tt>; in this case, it returns
that value.
</UL>
<P>
<LI> unconditional execution
<P>
<UL> <LI>
The <tt>do</tt> construct simply evaluates all forms in its clause.
<P>
<LI>
The <tt>return</tt> construct takes one form and returns its value. It is
equivalent to the clause <tt>do (return <i>value</i>)</tt>.
</UL>
<P>
<LI> conditional execution
<P>
<UL> <LI>
The <tt>if</tt> construct takes one form as a predicate and a clause that
is executed when the predicate is true. The clause can be a value
accumulation, unconditional, or another conditional clause; it can also
be any combination of such clauses connected by the loop keyword <tt>and</tt>.
<P>
<LI>
The <tt>when</tt> construct is a synonym for <tt>if</tt>.
<P>
<LI>
The <tt>unless</tt> construct is similar to <tt>when</tt> except that it complements
the predicate; it executes the following clause if the predicate is false.
<P>
<LI>
The <tt>else</tt> construct provides an optional component of <tt>if</tt>,
<tt>when</tt>, and <tt>unless</tt> clauses that is executed when the
predicate is false. The component is one of the clauses described under
<tt>if</tt>.
<P>
<LI>
The <tt>end</tt> construct provides an optional component to mark the
end of a conditional clause.
</UL>
<P>
<LI> miscellaneous operations
<P>
<UL> <LI> The <tt>named</tt> construct assigns a name to a loop construct.
<P>
<LI> The <tt>initially</tt> construct causes its forms to be evaluated
in the loop prologue, which precedes all loop code except for initial
settings specified by the constructs <tt>with</tt>, <tt>for</tt>, or <tt>as</tt>.
<P>
<LI> The <tt>finally</tt> construct causes its forms to be evaluated
in the loop epilogue after normal iteration terminates. An unconditional
clause can also follow the loop keyword <tt>finally</tt>.
</UL>
</UL>
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
<BR> <HR><A NAME=tex2html4570 HREF="node241.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html4568 HREF="node238.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html4562 HREF="node239.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html4572 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html4573 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html4571 HREF="node241.html"> Loop Syntax</A>
<B>Up:</B> <A NAME=tex2html4569 HREF="node238.html"> Parsing Loop Clauses</A>
<B> Previous:</B> <A NAME=tex2html4563 HREF="node239.html"> Order of Execution</A>
<HR> <P>
<HR>
<P><ADDRESS>
AI.Repository@cs.cmu.edu
</ADDRESS>
</BODY>