QinGW <········@21cn.com> wrote:
+---------------
| test code:
| >(apply #'list 1 nil)
| (1)
| >(funcall #'list 1 nil)
| (1 NIL)
|
| How to explain their difference?
+---------------
By noticing that they are *defined* differently in the CL spec,
especially the definition of the &REST parameters of each:
http://alu.org/HyperSpec/Body/fun_funcall.html
...
args---ARGUMENTS to the function.
versus:
http://alu.org/HyperSpec/Body/fun_apply.html
...
args---a SPREADABLE ARGUMENT LIST DESIGNATOR.
You may very well need to look up the terms emphasized above
in the Glossary:
http://alu.org/HyperSpec/Body/glo_a.html#argument
http://alu.org/HyperSpec/Body/glo_s.html#spreadable_argument_list_designator
In particular, read the second one *very* carefully...
-Rob
-----
Rob Warnock <····@rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607
> How to explain their difference?
Here's an answer specifically to this question:
http://www.cs.cmu.edu/Groups/AI/html/faqs/lang/lisp/part3/faq-doc-11.html
And you may find the rest of the FAQ --
http://www.cs.cmu.edu/Groups/AI/html/faqs/lang/lisp/part3/faq.html --
of interest to you, too.
QinGW wrote:
> test code:
>> (apply #'list 1 nil)
> (1)
>> (funcall #'list 1 nil)
> (1 NIL)
>
> How to explain their difference?
This is explained pretty well in the HyperSpec.
Pascal
--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
In article <··············@qingw.com>, QinGW <········@21cn.com> wrote:
> test code:
> >(apply #'list 1 nil)
> (1)
This is like (LIST 1)
1 is an arg.
The last is a list of args. In this case the empty list.
The arglist is constructed by consing the first args
in front of the last arg:
(cons arg1 (cons arg2 (cons arg3 argn) ...
So the new list of arguments is (CONS 1 NIL) -> (1).
LIST will be called with the arglist (1). -> (List 1).
> >(funcall #'list 1 nil)
> (1 NIL)
This is like (LIST 1 NIL).
LIST will be called with the arguments as in FUNCALL.
>
> How to explain their difference?
>
> qingwu wang
You could look at the Common Lisp Hyperspec.
APPLY:
http://www.lispworks.com/documentation/HyperSpec/Body/f_apply.htm#apply
FUNCALL:
http://www.lispworks.com/documentation/HyperSpec/Body/f_funcal.htm#funcall
--
http://lispm.dyndns.org
+ QinGW <········@21cn.com>:
| test code:
|>(apply #'list 1 nil)
| (1)
|>(funcall #'list 1 nil)
| (1 NIL)
|
| How to explain their difference?
APPLY takes the last argument, which must be a list, and makes the
elements of that list be the final arguments to the function.
So your first example is equivalent to (list 1), while the second is
equivalent to (list 1 nil). You might appreciate the difference more
if you replaced nil by '(2 3 4).
--
* Harald Hanche-Olsen <URL:http://www.math.ntnu.no/~hanche/>
- It is undesirable to believe a proposition
when there is no ground whatsoever for supposing it is true.
-- Bertrand Russell
QinGW wrote:
> test code:
>> (apply #'list 1 nil)
> (1)
>> (funcall #'list 1 nil)
> (1 NIL)
>
> How to explain their difference?
>
> qingwu wang
From http://www.lispworks.com/documentation/HyperSpec/Body/f_funcal.htm
Notes:
(funcall function arg1 arg2 ...)
== (apply function arg1 arg2 ... nil)
== (apply function (list arg1 arg2 ...))
----------
so, in your example, if you want to use "apply" and don't want to use
"list" you should add one more nil at the end.
CL-USER> (funcall #'list 1 nil)
(1 NIL)
CL-USER> (apply #'list 1 nil nil)
(1 NIL)
The reason why is this:
From http://www.lispworks.com/documentation/HyperSpec/Body/f_apply.htm
apply function &rest args+ => result*
[...]
args---a spreadable argument list designator
From
http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_s.htm#spreadable_argument_list_designator
spreadable argument list designator n. a designator for a list of
objects; that is, an object that denotes a list and that is a non-null
list L1 of length n, whose last element is a list L2 of length m
(denoting a list L3 of length m+n-1 whose elements are L1i for i < n-1
followed by L2j for j < m). ``The list (1 2 (3 4 5)) is a spreadable
argument list designator for the list (1 2 3 4 5).''
----------
In your example, L1 is '(1 nil), L2 is nil (i.e. the empty list) so L3
is '(1).
Benigno Ur�a
QinGW wrote:
> test code:
>> (apply #'list 1 nil)
is the same as: (apply #'list 1 '())
NIL and '() denotes 'empty list'
The last argument of APPLY must be a list of arguments.
NIL denotes an empty list, so you have 'list of NO arguments'.
So you actually provided only one argument to LIST, namely '1'
and got one-element list.
> (1)
>> (funcall #'list 1 nil)
> (1 NIL)
Here you provided two arguments, '1' and 'NIL', so you
got two-element list.