1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
<HTML>
<HEAD>
<TITLE>Common LISP Hints: Lambda</TITLE>
</HEAD>
<BODY>
<A HREF="LISP-tutorial-20.html"><IMG SRC="prev.gif" ALT="Previous"></A>
<A HREF="LISP-tutorial-22.html"><IMG SRC="next.gif" ALT="Next"></A>
<A HREF="LISP-tutorial.html#toc21"><IMG SRC="toc.gif" ALT="Contents"></A>
<HR>
<H2><A NAME="s21">21. Lambda</A></H2>
<P>If you just want to create a temporary function and don't want to
bother giving it a name, <CODE>lambda</CODE> is what you need.</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
> #'(lambda (x) (+ x 3))
(LAMBDA (X) (+ X 3))
> (funcall * 5) ;* is the last result,remember?
8
<tscreen><verb>
The combination of <tt/lambda/ and <tt/mapcar/ can replace many loops. For
example, the following two forms are equivalent:
<tscreen><verb>
> (do ((x '(1 2 3 4 5) (cdr x))
(y nil))
((null x) (reverse y))
(push (+ (car x) 2) y))
(3 4 5 6 7)
> (mapcar #'(lambda (x) (+ x 2)) '(1 2 3 4 5))
(3 4 5 6 7)
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>However the first version will run significantly faster when compiled
by CMUCL. But speed isn't everything and the second version is a lot
clearer and easier to debug.</P>
<HR>
<A HREF="LISP-tutorial-20.html"><IMG SRC="prev.gif" ALT="Previous"></A>
<A HREF="LISP-tutorial-22.html"><IMG SRC="next.gif" ALT="Next"></A>
<A HREF="LISP-tutorial.html#toc21"><IMG SRC="toc.gif" ALT="Contents"></A>
</BODY>
</HTML>
|