The defun
macro "stores" a function object in the function slot of a symbol, e.g.:
(defun f () "Hello.")
The function that prints "Hello" is now stored in the function slot of the symbol f
.
(And can be retrieved via #'f
.)
Is there another way (other than defun
) to write/"store" a function object in the function slot of a symbol?
Because something like (setf minus #'-)
only would store the function object in the value slot of the symbol minus
.
Asked
Active
Viewed 46 times
0

Frank
- 433
- 3
- 10
1 Answers
4
Is there another way (other than defun) to write/"store" a function object in the function slot of a symbol?
You can use symbol-function
:
(setf (symbol-function 'f) (lambda () "Bye."))
(f) ; => "Bye."
(defun g () "Hello again.")
(setf (symbol-function 'f) (symbol-function 'g))
(f) ; => "Hello again."
Another possibility is to use fdefinition
:
(setf (fdefinition 'f) (lambda () "Bye again.")
(f) ; => "By again"
They are almost equivalent. The main difference is that symbol-function
requires an argument which must be a symbol, while fdefinition
accepts a function name, which can be also a list (see the glossary).

Renzo
- 26,848
- 5
- 49
- 61
-
1also `fdefinition` – sds Dec 05 '16 at 21:46
-
@Renzo @sds Thank you! I always thought `symbol-function` ist only some kind of getter. But according to your example it can be used as a location in a setter as well. Going to check out the HyperSpec regarding `symbol-value`. Thanks again. – Frank Dec 05 '16 at 22:10
-
1Note that this only works for top level (global) bindings. It will ignore lexical bindings created in the function namespace with `flet` and `labels` – Sylwester Dec 05 '16 at 22:35