macrolyte wrote:I understand that *d*'s function value is set to the lambda form...
No, it's the
variable value of *d* that is set to the
function object returned by the lambda form:
Code: Select all
(setq *d* (lambda (r) (* 2 pi r)))
is the same as:
Code: Select all
(setf (symbol-value '*d*) (lambda (r) (* 2 pi r)))
Here
*d* is a variable pointing to a function object, because the
SYMBOL-VALUE slot of *d* points to the function object returned by the
LAMBDA macro. But a function object in a
SYMBOL-VALUE slot can only be used with
FUNCALL or
APPLY:
Code: Select all
(*d* 2) => error: undefined function
(funcall *d* 2) => 12.566370614359172d0
(apply *d* '(2)) => 12.566370614359172d0
The "undefined function" error happens because the
SYMBOL-VALUE slot of *d* and not the
SYMBOL-FUNCTION slot of *d* points ot the function object, what means that the function ist stored as the variable value of *d* and not as the function value of *d*.
The counter-example would be:
is the same as:
Code: Select all
(setf (symbol-function '*d*) (lambda (r) (* 2 pi r)))
Here
*d* is a function, because the
SYMBOL-FUNCTION slot of *d* points to a function object:
But a function as argument to
FUNCALL or
APPLY needs to be prefixed by #' because otherwise the
SYMBOL-VALUE slot is used:
Code: Select all
(funcall *d* 2) ; uses the symbol-value slot of *d*
(apply *d* '(2)) ; uses the symbol-value slot of *d*
(funcall #'*d* 2) ; uses the symbol-function slot of *d*
(apply #'*d* '(2)) ; uses the symbol-function slot of *d*
#' is a shortcut for the
FUNCTION special operator:
Code: Select all
(funcall (function *d*) 2) ; uses the symbol-function slot of *d*
(apply (function *d*) '(2)) ; uses the symbol-function slot of *d*
Summary:
In Common Lisp the
SYMBOL-FUNCTION as well as the
SYMBOL-VALUE slot can point to function objects (even two different function objects), but a function in a
SYMBOL-VALUE slot can only be used with
FUNCALL or
APPLY.
Often it's really handy that Common Lisp is a Lisp-2 (the same symbol name can be used as function and as variable), but sometimes things can become really complicated because of this.
- edgar