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

68 lines
4 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. Parsing Loop Clauses</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Parsing 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=tex2html4543 HREF="node239.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html4541 HREF="node235.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html4535 HREF="node237.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html4545 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html4546 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html4544 HREF="node239.html"> Order of Execution</A>
<B>Up:</B> <A NAME=tex2html4542 HREF="node235.html"> Loop</A>
<B> Previous:</B> <A NAME=tex2html4536 HREF="node237.html"> How the Loop </A>
<HR> <P>
<H1><A NAME=SECTION003030000000000000000>26.3. Parsing Loop Clauses</A></H1>
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
The syntactic parts of a loop construct are called <i>clauses</i>; the scope
of each clause is determined by the top-level parsing of that clause's
keyword. The following example shows a loop construct with six
clauses:
<P>
<P><pre>
(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 &quot;Working on ~D now&quot; i) ;Fourth clause
when (evenp i) ;Fifth clause
do (format t &quot;~D is a non-odd number&quot; i)
finally (format t &quot;About to exit!&quot;)) ;Sixth clause
</pre><P>
<P>
Each loop keyword introduces either a compound loop clause or a simple
loop clause that can consist of a loop keyword followed by a
single Lisp form. The number of
forms in a clause is determined by the loop keyword that begins the
clause and by the auxiliary keywords in the clause. The keywords
<tt>do</tt>, <tt>initially</tt>, and <tt>finally</tt> are the only loop
keywords that can take any number of Lisp forms and group them as if
in a single <tt>progn</tt> form.
<P>
Loop clauses can contain auxiliary keywords, which are sometimes
called <i>prepositions</i>. For example, the first clause in the preceding code
includes the prepositions <tt>from</tt> and <tt>to</tt>, which mark
the value from which stepping begins and the value at which stepping
ends.
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
<HR>
<UL>
<LI> <A NAME=tex2html4547 HREF="node239.html#SECTION003031000000000000000"> Order of Execution</A>
<LI> <A NAME=tex2html4548 HREF="node240.html#SECTION003032000000000000000"> Kinds of Loop Clauses</A>
<LI> <A NAME=tex2html4549 HREF="node241.html#SECTION003033000000000000000"> Loop Syntax</A>
</UL>
<BR> <HR><A NAME=tex2html4543 HREF="node239.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html4541 HREF="node235.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html4535 HREF="node237.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html4545 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html4546 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html4544 HREF="node239.html"> Order of Execution</A>
<B>Up:</B> <A NAME=tex2html4542 HREF="node235.html"> Loop</A>
<B> Previous:</B> <A NAME=tex2html4536 HREF="node237.html"> How the Loop </A>
<HR> <P>
<HR>
<P><ADDRESS>
AI.Repository@cs.cmu.edu
</ADDRESS>
</BODY>