File: LISP-tutorial-21.html

package info (click to toggle)
cmucl 20c-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 42,524 kB
  • sloc: lisp: 358,331; ansic: 28,385; asm: 3,777; sh: 1,236; makefile: 366; csh: 31
file content (46 lines) | stat: -rw-r--r-- 1,423 bytes parent folder | download | duplicates (12)
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>
&gt; #'(lambda (x) (+ x 3))
(LAMBDA (X) (+ X 3))
&gt; (funcall * 5)               ;* is the last result,remember?
8
&lt;tscreen&gt;&lt;verb&gt;

The combination of &lt;tt/lambda/ and &lt;tt/mapcar/ can replace many loops. For
example, the following two forms are equivalent:

&lt;tscreen&gt;&lt;verb&gt;
&gt; (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)
&gt; (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>