Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

apply & funcall - the different results

ANSI Common Lisp. Why I get an other answer in the last 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)
like image 260
Andrey Bushman Avatar asked Feb 14 '23 04:02

Andrey Bushman


1 Answers

APPLY expects as arguments:

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

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

Note that (list* '(1 2 3)) evaluates to just (1 2 3).

The arguments are called spreadable argument list 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)

APPLY uses such a spreadable argument list by design. For example (... 1 2 3 '(4 5)). With FUNCALL you have to write the arguments as usual: (... 1 2 3 4 5).

APPLY has a single purpose in Common Lisp: it allows functions to be called with computed argument lists. To make that a bit more convenient, this idea of the spreadable argument list has been used. It works the same for example in Emacs Lisp.

Imagine that you have a list of arguments and you want to add two arguments in front.

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

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

CL-USER 66 > (apply '+ (append (list 1 2) args))
63
like image 195
Rainer Joswig Avatar answered Mar 11 '23 10:03

Rainer Joswig