File: LISP-tutorial-20.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 (50 lines) | stat: -rw-r--r-- 1,853 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
47
48
49
50
<HTML>
<HEAD>
<TITLE>Common LISP Hints: Funcall, Apply, and Mapcar</TITLE>
</HEAD>
<BODY>
<A HREF="LISP-tutorial-19.html"><IMG SRC="prev.gif" ALT="Previous"></A>
<A HREF="LISP-tutorial-21.html"><IMG SRC="next.gif" ALT="Next"></A>
<A HREF="LISP-tutorial.html#toc20"><IMG SRC="toc.gif" ALT="Contents"></A>
<HR>
<H2><A NAME="s20">20. Funcall, Apply, and Mapcar</A></H2>

<P>Earlier I promised to give some functions which take functions as
arguments. Here they are:</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
&gt; (funcall #'+ 3 4)
7
&gt; (apply #'+ 3 4 '(3 4))
14
&gt; (mapcar #'not '(t nil t nil t nil))
(NIL T NIL T NIL T)
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P><CODE>Funcall</CODE> calls its first argument on its remaining arguments.</P>
<P><CODE>Apply</CODE> is just like <CODE>funcall</CODE>, except that its final argument
should be a 
list; the elements of that list are treated as if they were additional
arguments to a <CODE>funcall</CODE>.</P>
<P>The first argument to <CODE>mapcar</CODE> must be a function of one argument;
<CODE>mapcar</CODE> 
applies this function to each element of a list and collects the
results in another list.</P>
<P><CODE>Funcall</CODE> and <CODE>apply</CODE> are chiefly useful when their first argument is a
variable. For instance, a search engine could take a heuristic function
as a parameter and use <CODE>funcall</CODE> or <CODE>apply</CODE> to call that function on a
state description. The sorting functions described later use <CODE>funcall</CODE>
to call their comparison functions.</P>
<P><CODE>Mapcar</CODE>, along with nameless functions (see below), can replace many
loops.</P>



<HR>
<A HREF="LISP-tutorial-19.html"><IMG SRC="prev.gif" ALT="Previous"></A>
<A HREF="LISP-tutorial-21.html"><IMG SRC="next.gif" ALT="Next"></A>
<A HREF="LISP-tutorial.html#toc20"><IMG SRC="toc.gif" ALT="Contents"></A>
</BODY>
</HTML>