Apply & funcall - different results

ANSI Common Lisp. Why do I get a different answer in the latter case?

(list 1 2 3 nil) ; (1 2 3 nil)
(funcall (function list) 1 2 3 nil) ; (1 2 3 nil)
(apply (function list) '(1 2 3 nil)) ; (1 2 3 nil)
(apply (function list) 1 2 3 nil) ; (1 2 3)
+4
source share
1 answer

APPLY expects as arguments:

  • function
  • zero ... n arguments
  • and then the argument list at the end

The function will basically be called with the result (list* 0-arg ... n-arg argument-list)

Please note that it (list* '(1 2 3))is rated just (1 2 3).

Arguments are called extensible argument lists in Common Lisp.

CL-USER 60 > (apply (function list) 1 2 3 nil)
(1 2 3)

CL-USER 61 > (apply (function list) (list* 1 2 3 nil))
(1 2 3)

CL-USER 62 > (apply (function list) (list* '(1 2 3)))
(1 2 3)

APPLYuses such an extensible list of design arguments. For example (... 1 2 3 '(4 5)). With the help FUNCALLyou need to write the arguments as usual (... 1 2 3 4 5).

APPLY Common Lisp: . , . , , Emacs Lisp.

, , .

CL-USER 64 > (apply '+ args)
60

CL-USER 65 > (apply '+ 1 2 args)
63

CL-USER 66 > (apply '+ (append (list 1 2) args))
63
+8

All Articles