emacs.d/clones/lisp/colinallen.dnsalias.org/lp/node90.html

127 lines
4.8 KiB
HTML
Raw Normal View History

2022-08-02 12:34:59 +02:00
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
<!Originally converted to HTML using LaTeX2HTML 95 (Thu Jan 19 1995) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
<HEAD>
<TITLE> do (SPECIAL FORM)</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" do (SPECIAL FORM)">
<meta name="keywords" value="lp">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<BR> <HR>
<A HREF="node91.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A>
<A HREF="node72.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A>
<A HREF="node89.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif"></A> <BR>
<A HREF="lp.html"><B>Contents</B></A>
<B> Next:</B>
<A HREF="node91.html"> documentation (FUNCTION)</A>
<B>Up:</B>
<A HREF="node72.html"> Appendix: Selected Lisp </A>
<B> Previous:</B>
<A HREF="node89.html"> defun (MACRO)</A>
<BR> <HR> <P>
<H1> do (SPECIAL FORM)</H1>
<P>
<b> Format:</b>
<tt> (do ((&lt;var1&gt; &lt;init1&gt; &lt;update1&gt; )</tt>
<tt> (&lt;var2&gt; &lt;init2&gt; &lt;update2&gt; )</tt>
<tt> .</tt>
<tt> .</tt>
<tt> (&lt;varN&gt; &lt;initN&gt; &lt;updateN&gt; ))</tt>
<tt> (&lt;test&gt; &lt;body1&gt; )</tt>
<tt> &lt;body2&gt; )</tt>
<P>
<b> Required arguments:</b>
2
<P>
<tt> ((&lt;var&gt; &lt;init&gt; &lt;update&gt; )...)</tt>: a list of zero or more variable
clauses; <tt> &lt;var&gt; </tt> is a symbol appropriate as a variable
name; <tt> &lt;init&gt; </tt>, which is optional, is any Lisp
expression; <tt> &lt;update&gt; </tt>, which is optional, is any Lisp
expression.
<tt> (&lt;test&gt; &lt;body1&gt; )</tt>: <tt> &lt;test&gt; </tt> is any Lisp expression;
<tt> &lt;body1&gt; </tt> is a sequence of zero or more Lisp
expressions.
<P>
<b> Optional arguments:</b>
arbitrary
<P>
<tt> &lt;body2&gt; </tt>: a sequence of zero or more Lisp expressions
<P>
The special form do allows the programmer to specify
iteration. The first part of do is a list of variables; if <tt> &lt;init&gt; </tt>
is provided, the <tt> &lt;var&gt; </tt>'s are initialised to the result of evaluating
the corresponding <tt> &lt;init&gt; </tt>. If no <tt> &lt;init&gt; </tt> is provided, <tt> &lt;var&gt; </tt> is
initialised to NIL. The optional <tt> &lt;update&gt; </tt> expression may
specify how the variable is to be modified after every iteration.
After every iteration, <tt> &lt;var&gt; </tt> is set to result of evaluating
<tt> &lt;update&gt; </tt>.
Initialisation and updating for all variables is performed in
parallel; thus a <tt> &lt;var&gt; </tt> in one clause may not be used in the <tt>
&lt;init&gt; </tt> or <tt> &lt;update&gt; </tt> of another clause.
<P>
The second part of ``do,'' the <tt> &lt;test&gt; </tt>, checks for termination. The
<tt> &lt;test&gt; </tt> is evaluated before each pass; if it returns a non-NIL value,
the sequence of expressions in <tt> &lt;body1&gt; </tt> are evaluated one by one; do
returns the value of the last expression in <tt> &lt;body1&gt; </tt>. If <tt> &lt;body1&gt; </tt>
contains no expressions, do returns NIL.
<P>
The third part of the
do is the body of the iteration, <tt> &lt;body2&gt; </tt>. At each pass, the sequence
of expressions in <tt> &lt;body2&gt; </tt> are evaluated one by one.
If <tt> &lt;body2&gt; </tt> contains an expression of the form <tt> (return &lt;expr&gt; )</tt>, where
<tt> &lt;expr&gt; </tt> is any Lisp expression, do terminates immediately and returns
the result of evaluating <tt>&lt;expr&gt; </tt>.
<P>
<b> Examples:</b>
<P>
<BLOCKQUOTE>
<PRE> (setq sum (+ sum (first lst))))
25
&gt; (do ((lst
'(a (b (c d)) e (f))
(rest lst))
(len 0 (+ 1 len))) ; determines length of lst
((null lst) len)) ; &quot;do,&quot; here, has no body
4
&gt; (defun my-exp (m n) ; raise m to power of n
(do ((result 1)
(exp n))
((= exp 0) result)
(setq result (* result m))
(setq exp (- exp 1))))
MY-EXP
&gt; (my-exp 5 3)
125
&gt; (defun my-exp2 (m n) ; simpler version of my-exp
(do ((result 1 (* result m))
(exp n (- exp 1)))
((= exp 0) result)))
</PRE>
</BLOCKQUOTE>
<P>
<BR> <HR>
<A HREF="node91.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A>
<A HREF="node72.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A>
<A HREF="node89.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif"></A> <BR>
<A HREF="lp.html"><B>Contents</B></A>
<B> Next:</B>
<A HREF="node91.html"> documentation (FUNCTION)</A>
<B>Up:</B>
<A HREF="node72.html"> Appendix: Selected Lisp </A>
<B> Previous:</B>
<A HREF="node89.html"> defun (MACRO)</A>
<BR> <HR> <P>
<BR> <HR>
<P>
<ADDRESS>
<I>&#169; Colin Allen &amp; Maneesh Dhagat <BR>
November 1999</I>
</ADDRESS>
</BODY>