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

113 lines
4.8 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.11. Unconditional Execution</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Unconditional Execution">
<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=tex2html4676 HREF="node250.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html4674 HREF="node235.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html4668 HREF="node248.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html4678 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html4679 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html4677 HREF="node250.html"> Miscellaneous Features</A>
<B>Up:</B> <A NAME=tex2html4675 HREF="node235.html"> Loop</A>
<B> Previous:</B> <A NAME=tex2html4669 HREF="node248.html"> Conditional Execution</A>
<HR> <P>
<H1><A NAME=SECTION0030110000000000000000>26.11. Unconditional Execution</A></H1>
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
<A NAME=LOOPUNCONDSECTION>The</A>
loop construct <tt>do</tt> (or <tt>doing</tt>) takes one or more expressions
and simply evaluates them in order.
<P>
The loop construct <tt>return</tt> takes one expression and returns its value. It
is equivalent to the clause <tt>do (return <i>value</i>)</tt>.
<P>
<BR><b>[Loop Clause]</b><BR>
<tt>do</tt> <tt>{<i>expr</i>}*</tt> <tt><BR></tt><tt>doing</tt> <tt>{<i>expr</i>}*</tt><P>The <tt>do</tt> construct simply evaluates the specified expressions
wherever they occur in the expanded form of <tt>loop</tt>.
<P>
The <i>expr</i> argument can be any non-atomic Common Lisp form.
Each <i>expr</i> is evaluated in every iteration.
<P>
The constructs <tt>do</tt>, <tt>initially</tt>, and <tt>finally</tt> are the
only loop keywords that take an arbitrary number of forms and group
them as if using an implicit <tt>progn</tt>.
Because every loop clause must begin with a loop keyword, you would use
the keyword <tt>do</tt> when no control action other than execution is
required.
<P>
Examples:
<P><pre>
;;; Print some numbers.
(loop for i from 1 to 5
do (print i)) `;Prints 5 lines
1
2
3
4
5
=> NIL
;;; Print numbers and their squares.
;;; The DO construct applies to multiple forms.
(loop for i from 1 to 4
do (print i)
(print (* i i))) `;Prints 8 lines
1
1
2
4
3
9
4
16
=> NIL
</pre><P>
<P>
<BR><b>[Loop Clause]</b><BR>
<tt>return</tt> <em>expr</em><P>The <tt>return</tt> construct terminates a
loop and returns the value of
the specified expression as the value of the loop. This construct
is similar to the Common Lisp special form <tt>return-from</tt> and the
Common Lisp macro <tt>return</tt>.
<P>
The Loop Facility supports the <tt>return</tt> construct for backward
compatibility with older <tt>loop</tt> implementations.
The <tt>return</tt> construct returns immediately and does not execute
any <tt>finally</tt> clause that is given.
<P>
Examples:
<P><pre>
;;; Signal an exceptional condition.
(loop for item in '(1 2 3 a 4 5)
when (not (numberp item))
return (cerror &quot;enter new value&quot;
&quot;non-numeric value: ~s&quot;
item)) `;Signals an error
&gt;&gt;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 &quot;enter new value&quot;
&quot;non-numeric value: ~s&quot;
item))) `;Signals an error
&gt;&gt;Error: non-numeric value: A
</pre><P>
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
<BR> <HR><A NAME=tex2html4676 HREF="node250.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html4674 HREF="node235.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html4668 HREF="node248.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html4678 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html4679 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html4677 HREF="node250.html"> Miscellaneous Features</A>
<B>Up:</B> <A NAME=tex2html4675 HREF="node235.html"> Loop</A>
<B> Previous:</B> <A NAME=tex2html4669 HREF="node248.html"> Conditional Execution</A>
<HR> <P>
<HR>
<P><ADDRESS>
AI.Repository@cs.cmu.edu
</ADDRESS>
</BODY>