1
0
Fork 0
cl-sites/www.cs.cmu.edu/Groups/AI/html/cltl/clm/node89.html

162 lines
7.5 KiB
HTML
Raw Normal View History

2023-10-25 11:23:21 +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>7.8.3. Simple Iteration Constructs</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Simple Iteration Constructs">
<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=tex2html2637 HREF="node90.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html2635 HREF="node86.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html2629 HREF="node88.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html2639 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html2640 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html2638 HREF="node90.html"> Mapping</A>
<B>Up:</B> <A NAME=tex2html2636 HREF="node86.html"> Iteration</A>
<B> Previous:</B> <A NAME=tex2html2630 HREF="node88.html"> General Iteration</A>
<HR> <P>
<H2><A NAME=SECTION001183000000000000000>7.8.3. Simple Iteration Constructs</A></H2>
<P>
The constructs <tt>dolist</tt> and <tt>dotimes</tt> execute a body of code
once for each value taken by a single variable. They are expressible
in terms of <tt>do</tt>, but capture very common patterns of use.
<P>
Both <tt>dolist</tt> and <tt>dotimes</tt> perform
a body of statements repeatedly. On each iteration a specified
variable is bound to an element of interest that the body may
examine. <tt>dolist</tt> examines successive elements of a list,
and <tt>dotimes</tt> examines integers from 0 to <i>n</i>-1
for some specified positive integer <i>n</i>.
<P>
The value of any of these constructs may be specified by an optional result
form, which if omitted defaults to the value <tt>nil</tt>.
<P>
The <tt>return</tt> statement may be used to return
immediately from a <tt>dolist</tt> or <tt>dotimes</tt> form,
discarding any following iterations
that might have been performed; in effect, a <tt>block</tt> named <tt>nil</tt>
surrounds the construct.
The body of the loop is implicitly a <tt>tagbody</tt> construct;
it may contain tags to serve as the targets of <tt>go</tt> statements.
Declarations may appear before the body of the loop.
<P>
<BR><b>[Macro]</b><BR>
<pre>
dolist (<i>var</i> <i>listform</i> [<i>resultform</i>])
{<i>declaration</i>}* {<i>tag</i> | <i>statement</i>}*
</pre>
<P><tt>dolist</tt> provides straightforward iteration over the elements of a list.
First <tt>dolist</tt>
evaluates the form <i>listform</i>,
which should produce a list. It then executes the body
once for each element in the list, in order, with
the variable <i>var</i> bound to the element.
Then <i>resultform</i> (a single form, <i>not</i> an implicit <tt>progn</tt>)
is evaluated, and the result is the value of the <tt>dolist</tt>
form. (When the <i>resultform</i> is evaluated, the control variable <i>var</i>
is still bound and has the value <tt>nil</tt>.)
If <i>resultform</i> is omitted, the result is <tt>nil</tt>.
<P>
<P><pre>
(dolist (x '(a b c d)) (prin1 x) (princ &quot; &quot;)) => <tt>nil</tt>
after printing ``<tt>a b c d </tt>'' (note the trailing space)
</pre><P>
<P>
An explicit <tt>return</tt> statement may be used to terminate the loop
and return a specified value.
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
X3J13 voted in January 1989
(MAPPING-DESTRUCTIVE-INTERACTION) <A NAME=7341>&#160;</A>
to restrict user side effects; see section <A HREF="node92.html#STRUCTURETRAVERSALSECTION">7.9</A>.
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
<BR><b>[Macro]</b><BR>
<pre>
dotimes (<i>var</i> <i>countform</i> [<i>resultform</i>])
{<i>declaration</i>}* {<i>tag</i> | <i>statement</i>}*
</pre>
<P><tt>dotimes</tt> provides straightforward iteration over a sequence of integers.
The expression
<tt>(dotimes (<i>var</i> <i>countform</i> <i>resultform</i>) . <i>progbody</i>)</tt>
evaluates the form <i>countform</i>, which should produce an integer. It then
performs <i>progbody</i> once for each integer from zero (inclusive) to
<i>count</i> (exclusive), in order, with the variable <i>var</i> bound to the
integer; if the value of <i>countform</i> is zero or negative,
then the <i>progbody</i> is
performed zero times. Finally, <i>resultform</i> (a single form, <i>not</i> an
implicit <tt>progn</tt>) is evaluated, and the result is the value of the
<tt>dotimes</tt> form. (When the <i>resultform</i> is evaluated, the control
variable <i>var</i> is still bound and has as its value the number of times
the body was executed.)
If <i>resultform</i> is omitted, the result is <tt>nil</tt>.
<P>
An explicit <tt>return</tt> statement may be used to terminate the loop
and return a specified value.
<P>
Here is an example of the use of <tt>dotimes</tt> in processing strings:
<P><pre>
;;; True if the specified subsequence of the string is a
;;; palindrome (reads the same forwards and backwards).
(defun palindromep (string <tt>&amp;optional</tt>
(start 0)
(end (length string)))
(dotimes (k (floor (- end start) 2) <tt>t</tt>)
(unless (char-equal (char string (+ start k))
(char string (- end k 1)))
(return <tt>nil</tt>))))
(palindromep &quot;Able was I ere I saw Elba&quot;) => <tt>t</tt>
(palindromep &quot;A man, a plan, a canal-Panama!&quot;) => <tt>nil</tt>
(remove-if-not #'alpha-char-p ;Remove punctuation
&quot;A man, a plan, a canal-Panama!&quot;)
=> &quot;AmanaplanacanalPanama&quot;
(palindromep
(remove-if-not #'alpha-char-p
&quot;A man, a plan, a canal-Panama!&quot;)) => <tt>t</tt>
(palindromep
(remove-if-not
#'alpha-char-p
&quot;Unremarkable was I ere I saw Elba Kramer, nu?&quot;)) => <tt>t</tt>
(palindromep
(remove-if-not
#'alpha-char-p
&quot;A man, a plan, a cat, a ham, a yak,
a yam, a hat, a canal-Panama!&quot;)) => <tt>t</tt>
(palindromep
(remove-if-not
#'alpha-char-p
&quot;Ja-da, ja-da, ja-da ja-da jing jing jing&quot;)) => <tt>nil</tt>
</pre><P>
<P>
Altering the value of <i>var</i> in the body of the loop (by using <tt>setq</tt>,
for example) will have unpredictable, possibly implementation-dependent
results. A Common Lisp compiler may choose to issue a warning if such a variable
appears in a <tt>setq</tt>.
<P>
<hr>
<b>Compatbility note:</b> The <tt>dotimes</tt> construct is the closest thing
in Common Lisp to the Interlisp <tt>rptq</tt> construct.
<hr>
<P>
See also <tt>do-symbols</tt>, <tt>do-external-symbols</tt>,
and <tt>do-all-symbols</tt>.
<P>
<BR> <HR><A NAME=tex2html2637 HREF="node90.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html2635 HREF="node86.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html2629 HREF="node88.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html2639 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html2640 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html2638 HREF="node90.html"> Mapping</A>
<B>Up:</B> <A NAME=tex2html2636 HREF="node86.html"> Iteration</A>
<B> Previous:</B> <A NAME=tex2html2630 HREF="node88.html"> General Iteration</A>
<HR> <P>
<HR>
<P><ADDRESS>
AI.Repository@cs.cmu.edu
</ADDRESS>
</BODY>