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

80 lines
4.4 KiB
HTML

<!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.2. How the Loop Facility Works</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" How the Loop Facility Works">
<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=tex2html4531 HREF="node238.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html4529 HREF="node235.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html4523 HREF="node236.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html4533 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html4534 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html4532 HREF="node238.html"> Parsing Loop Clauses</A>
<B>Up:</B> <A NAME=tex2html4530 HREF="node235.html"> Loop</A>
<B> Previous:</B> <A NAME=tex2html4524 HREF="node236.html"> Introduction</A>
<HR> <P>
<H1><A NAME=SECTION003020000000000000000>26.2. How the Loop Facility Works</A></H1>
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
The driving element of the Loop Facility is the <tt>loop</tt> macro.
When Lisp encounters a <tt>loop</tt> macro call
form, it invokes the Loop Facility and passes to it the loop clauses
as a list of unevaluated forms, as with any macro.
The loop clauses contain Common Lisp forms and loop keywords. The
loop keywords are recognized by their symbol name, regardless of the
packages that contain them. The <tt>loop</tt> macro translates the
given form into Common Lisp code and returns the expanded form.
<P>
The expanded loop form is
one or more lambda-expressions for the local binding of loop variables
and a block and a tagbody that express a looping control structure.
The variables established in the loop construct are bound as
if by using <tt>let</tt> or <tt>lambda</tt>. Implementations can interleave the
setting of initial values with the bindings. However, the assignment
of the initial values is always calculated in the order specified by
the user. A variable is thus sometimes bound to a harmless value of the
correct data type, and then later in the prologue it is set to the true
initial value by using <tt>setq</tt>.
<P>
The expanded form consists of three basic parts in the tagbody:
<P>
<UL><LI>
The <i>loop prologue</i> contains forms that are executed before iteration begins,
such as initial settings of loop variables and possibly an initial
termination test.
<P>
<LI>
The <i>loop body</i> contains those forms that are executed during iteration,
including application-specific calculations, termination tests,
and variable stepping. <i>Stepping</i> is the process of assigning a
variable the next item in a series of items.
<P>
<LI>
The <i>loop epilogue</i> contains forms that are executed after iteration
terminates,
such as code to return values from the loop.
</UL>
<P>
Expansion of the <tt>loop</tt> macro produces an implicit block
(named <tt>nil</tt>).
Thus, the Common Lisp macro <tt>return</tt> and the special form
<tt>return-from</tt> can be
used to return values from a loop or to exit a loop.
<P>
Within the executable parts of loop clauses and around the entire
loop form, you can still bind variables by using the Common Lisp
special form <tt>let</tt>.
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
<BR> <HR><A NAME=tex2html4531 HREF="node238.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html4529 HREF="node235.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html4523 HREF="node236.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html4533 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html4534 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html4532 HREF="node238.html"> Parsing Loop Clauses</A>
<B>Up:</B> <A NAME=tex2html4530 HREF="node235.html"> Loop</A>
<B> Previous:</B> <A NAME=tex2html4524 HREF="node236.html"> Introduction</A>
<HR> <P>
<HR>
<P><ADDRESS>
AI.Repository@cs.cmu.edu
</ADDRESS>
</BODY>