127 lines
4.8 KiB
HTML
127 lines
4.8 KiB
HTML
|
<!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 ((<var1> <init1> <update1> )</tt>
|
||
|
<tt> (<var2> <init2> <update2> )</tt>
|
||
|
<tt> .</tt>
|
||
|
<tt> .</tt>
|
||
|
<tt> (<varN> <initN> <updateN> ))</tt>
|
||
|
<tt> (<test> <body1> )</tt>
|
||
|
<tt> <body2> )</tt>
|
||
|
<P>
|
||
|
<b> Required arguments:</b>
|
||
|
2
|
||
|
<P>
|
||
|
<tt> ((<var> <init> <update> )...)</tt>: a list of zero or more variable
|
||
|
clauses; <tt> <var> </tt> is a symbol appropriate as a variable
|
||
|
name; <tt> <init> </tt>, which is optional, is any Lisp
|
||
|
expression; <tt> <update> </tt>, which is optional, is any Lisp
|
||
|
expression.
|
||
|
<tt> (<test> <body1> )</tt>: <tt> <test> </tt> is any Lisp expression;
|
||
|
<tt> <body1> </tt> is a sequence of zero or more Lisp
|
||
|
expressions.
|
||
|
<P>
|
||
|
<b> Optional arguments:</b>
|
||
|
arbitrary
|
||
|
<P>
|
||
|
<tt> <body2> </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> <init> </tt>
|
||
|
is provided, the <tt> <var> </tt>'s are initialised to the result of evaluating
|
||
|
the corresponding <tt> <init> </tt>. If no <tt> <init> </tt> is provided, <tt> <var> </tt> is
|
||
|
initialised to NIL. The optional <tt> <update> </tt> expression may
|
||
|
specify how the variable is to be modified after every iteration.
|
||
|
After every iteration, <tt> <var> </tt> is set to result of evaluating
|
||
|
<tt> <update> </tt>.
|
||
|
Initialisation and updating for all variables is performed in
|
||
|
parallel; thus a <tt> <var> </tt> in one clause may not be used in the <tt>
|
||
|
<init> </tt> or <tt> <update> </tt> of another clause.
|
||
|
<P>
|
||
|
The second part of ``do,'' the <tt> <test> </tt>, checks for termination. The
|
||
|
<tt> <test> </tt> is evaluated before each pass; if it returns a non-NIL value,
|
||
|
the sequence of expressions in <tt> <body1> </tt> are evaluated one by one; do
|
||
|
returns the value of the last expression in <tt> <body1> </tt>. If <tt> <body1> </tt>
|
||
|
contains no expressions, do returns NIL.
|
||
|
<P>
|
||
|
The third part of the
|
||
|
do is the body of the iteration, <tt> <body2> </tt>. At each pass, the sequence
|
||
|
of expressions in <tt> <body2> </tt> are evaluated one by one.
|
||
|
If <tt> <body2> </tt> contains an expression of the form <tt> (return <expr> )</tt>, where
|
||
|
<tt> <expr> </tt> is any Lisp expression, do terminates immediately and returns
|
||
|
the result of evaluating <tt><expr> </tt>.
|
||
|
<P>
|
||
|
<b> Examples:</b>
|
||
|
<P>
|
||
|
<BLOCKQUOTE>
|
||
|
<PRE> (setq sum (+ sum (first lst))))
|
||
|
25
|
||
|
|
||
|
> (do ((lst
|
||
|
'(a (b (c d)) e (f))
|
||
|
(rest lst))
|
||
|
(len 0 (+ 1 len))) ; determines length of lst
|
||
|
((null lst) len)) ; "do," here, has no body
|
||
|
4
|
||
|
|
||
|
> (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
|
||
|
|
||
|
> (my-exp 5 3)
|
||
|
125
|
||
|
|
||
|
> (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>© Colin Allen & Maneesh Dhagat <BR>
|
||
|
November 1999</I>
|
||
|
</ADDRESS>
|
||
|
</BODY>
|