Search code examples
common-lispon-lisp

Difference between (apply #'somefunc args) and (somefunc args)


While reading Paul Graham's On Lisp I found the following function in Chapter 4, Utility Functions.

(defun symb (&rest args)
  (values (intern (apply #'mkstr args)))) ;; mkstr function is "applied"
;; which evaluates like in the following example:
> (symb nil T :a)
NILTA

I would like to understand what is the difference with the following function, slightly different:

(defun symb1 (&rest args)
  (values (intern (mkstr args)))) ;; directly calling mkstr
;; which evaluates like in the following example:
> (symb1 nil T :a)
|(NIL T A)|

In this second version, mkstr is directly evaluated with args arguments, but I don't understand why we need to do (apply #'mkstr ...) in the original.


Solution

  • When you call (f args), you call f with one argument.

    With (apply #'f args), you call f with as many arguments as the args list contains. So if args is (1 2), then (apply #'f args) is equivalent to (f 1 2).

    See APPLY.