124 lines
6.4 KiB
HTML
124 lines
6.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>7.3. Function Invocation</TITLE>
|
|
</HEAD>
|
|
<BODY>
|
|
<meta name="description" value=" Function Invocation">
|
|
<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=tex2html2536 HREF="node82.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html2534 HREF="node76.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html2528 HREF="node80.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html2538 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html2539 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
|
|
<B> Next:</B> <A NAME=tex2html2537 HREF="node82.html"> Simple Sequencing</A>
|
|
<B>Up:</B> <A NAME=tex2html2535 HREF="node76.html"> Control Structure</A>
|
|
<B> Previous:</B> <A NAME=tex2html2529 HREF="node80.html"> Generalized Variables</A>
|
|
<HR> <P>
|
|
<H1><A NAME=SECTION001130000000000000000>7.3. Function Invocation</A></H1>
|
|
<P>
|
|
The most primitive form for function invocation in Lisp of course
|
|
has no name; any list that has no other interpretation
|
|
as a macro call or special form is taken to be a function call.
|
|
Other constructs are provided for less common but
|
|
nevertheless frequently useful situations.
|
|
<P>
|
|
<BR><b>[Function]</b><BR>
|
|
<tt>apply</tt> <tt><i>function arg</i></tt> <tt>&rest</tt> <tt><i>more-args</i></tt><P>This applies <i>function</i> to a list of arguments.<p>
|
|
<img align=bottom alt="old_change_begin" src="gif/old_change_begin.gif"><br>
|
|
The <i>function</i> may be a
|
|
compiled-code object, or a lambda-expression, or a symbol; in the latter
|
|
case the global functional value of that symbol is used (but it is
|
|
illegal for the symbol to be the name of a macro or special form).
|
|
<br><img align=bottom alt="old_change_end" src="gif/old_change_end.gif">
|
|
<P>
|
|
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
|
|
X3J13 voted in June 1988 (FUNCTION-TYPE) <A NAME=6118> </A> to allow the <i>function</i>
|
|
to be only of type <tt>symbol</tt> or <tt>function</tt>; a lambda-expression
|
|
is no longer acceptable as a functional argument. One must use the
|
|
<tt>function</tt> special form or the abbreviation <tt>#'</tt> before
|
|
a lambda-expression that appears as an explicit argument form.
|
|
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
|
|
<P>
|
|
The arguments for the <i>function</i> consist of the last argument
|
|
to <tt>apply</tt> appended to the end of a list of all the other
|
|
arguments to <tt>apply</tt> but the <i>function</i> itself;
|
|
it is as if all the arguments to <tt>apply</tt> except the <i>function</i>
|
|
were given to <tt>list*</tt> to create the argument list.
|
|
For example:
|
|
<P><pre>
|
|
(setq f '+) (apply f '(1 2)) => 3
|
|
(setq f #'-) (apply f '(1 2)) => -1
|
|
(apply #'max 3 5 '(2 7 3)) => 7
|
|
(apply 'cons '((+ 2 3) 4)) =>
|
|
((+ 2 3) . 4) <i>not</i> (5 . 4)
|
|
(apply #'+ '()) => 0
|
|
</pre><P>
|
|
Note that if the function takes keyword arguments, the
|
|
keywords as well as the corresponding values must appear in the argument
|
|
list:
|
|
<P><pre>
|
|
(apply #'(lambda (<tt>&key</tt> a b) (list a b)) '(:b 3)) => (<tt>nil</tt> 3)
|
|
</pre><P>
|
|
This can be very useful in conjunction with the <tt>&allow-other-keys</tt> feature:
|
|
<P><pre>
|
|
(defun foo (size <tt>&rest</tt> keys <tt>&key</tt> double <tt>&allow-other-keys</tt>)
|
|
(let ((v (apply #'make-array size :allow-other-keys t keys)))
|
|
(if double (concatenate (type-of v) v v) v)))
|
|
|
|
(foo 4 :initial-contents '(a b c d) :double t)
|
|
=> #(a b c d a b c d)
|
|
</pre><P>
|
|
<P>
|
|
<BR><b>[Function]</b><BR>
|
|
<tt>funcall</tt> <tt><i>fn</i></tt> <tt>&rest</tt> <tt><i>arguments</i></tt><P><tt>(funcall <i>fn</i> <i>a1</i> <i>a2</i> ... <i>an</i>)</tt>
|
|
applies the function <i>fn</i> to the arguments
|
|
<i>a1</i>, <i>a2</i>, ..., <i>an</i>.
|
|
The <i>fn</i> may not
|
|
be a special form or a macro; this would not be meaningful.
|
|
<P>
|
|
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
|
|
X3J13 voted in June 1988 (FUNCTION-TYPE) <A NAME=6158> </A> to allow the <i>fn</i>
|
|
to be only of type <tt>symbol</tt> or <tt>function</tt>; a lambda-expression
|
|
is no longer acceptable as a functional argument. One must use the
|
|
<tt>function</tt> special form or the abbreviation <tt>#'</tt> before
|
|
a lambda-expression that appears as an explicit argument form.
|
|
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
|
|
<P>
|
|
For example:
|
|
<P><pre>
|
|
(cons 1 2) => (1 . 2)
|
|
(setq cons (symbol-function '+))
|
|
(funcall cons 1 2) => 3
|
|
</pre><P>
|
|
The difference between <tt>funcall</tt> and an ordinary function call is that
|
|
the function is obtained by ordinary Lisp evaluation rather than
|
|
by the special interpretation of the function position that normally
|
|
occurs.
|
|
<P>
|
|
<hr>
|
|
<b>Compatibility note:</b> The Common Lisp function <tt>funcall</tt> corresponds roughly to
|
|
the Interlisp primitive <tt>apply*</tt>.
|
|
<hr>
|
|
<P>
|
|
<BR><b>[Constant]</b><BR>
|
|
<tt>call-arguments-limit</tt><P>The value of <tt>call-arguments-limit</tt> is a positive integer that is
|
|
the upper exclusive bound on the number of arguments that may
|
|
be passed to a function. This bound depends on the implementation
|
|
but will not be smaller than 50.
|
|
(Implementors are encouraged to make this limit as large as practicable
|
|
without sacrificing performance.)
|
|
The value of <tt>call-arguments-limit</tt> must be at
|
|
least as great as that of <tt>lambda-parameters-limit</tt>.
|
|
See also <tt>multiple-values-limit</tt>.
|
|
<P>
|
|
<BR> <HR><A NAME=tex2html2536 HREF="node82.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html2534 HREF="node76.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html2528 HREF="node80.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html2538 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html2539 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
|
|
<B> Next:</B> <A NAME=tex2html2537 HREF="node82.html"> Simple Sequencing</A>
|
|
<B>Up:</B> <A NAME=tex2html2535 HREF="node76.html"> Control Structure</A>
|
|
<B> Previous:</B> <A NAME=tex2html2529 HREF="node80.html"> Generalized Variables</A>
|
|
<HR> <P>
|
|
<HR>
|
|
<P><ADDRESS>
|
|
AI.Repository@cs.cmu.edu
|
|
</ADDRESS>
|
|
</BODY>
|