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

48 lines
2.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>B.2. Generators</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Generators">
<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=tex2html6250 HREF="node365.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html6248 HREF="node362.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html6242 HREF="node363.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html6252 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html6253 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html6251 HREF="node365.html"> Gatherers</A>
<B>Up:</B> <A NAME=tex2html6249 HREF="node362.html"> Generators and Gatherers</A>
<B> Previous:</B> <A NAME=tex2html6243 HREF="node363.html"> Introduction</A>
<HR> <P>
<H1><A NAME=SECTION003520000000000000000>B.2. Generators</A></H1>
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
These functions create and process generators.
<P>
<BR><b>[Function]</b><BR>
<tt>generator <i>series</i></tt><P>Given a series, <tt>generator</tt> returns a generator containing the same
elements.
<P>
<BR><b>[Macro]</b><BR>
<tt>next-in <i>generator</i> {<i>action</i>}*</tt><P><tt>next-in</tt> returns the next element in the generator <i>generator</i>.
The <i>actions</i> can be any Lisp expressions. They are evaluated if and
only if no more elements can be retrieved from <i>generator</i>. If there
are no more elements and no actions, it is an error. It is also an error
to apply <tt>next-in</tt> to a generator a second time after the generator has
run out of elements. As an example of generators, consider the following.
<P><pre>
(let ((x (generator (scan '(1 2 3 4)))))
(with-output-to-string (s)
(loop (prin1 (next-in x (return)) s)
(prin1 (next-in x (return)) s)
(princ &quot;,&quot; s))))
=> &quot;12,34,&quot;
</pre><P>
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
<HR>
<P><ADDRESS>
AI.Repository@cs.cmu.edu
</ADDRESS>
</BODY>