Next: Obsolete Functions, Previous: Anonymous Functions, Up: Functions
The function definition of a symbol is the object stored in the function cell of the symbol. The functions described here access, test, and set the function cell of symbols.
See also the function indirect-function. See Definition of indirect-function.
This returns the object in the function cell of symbol. If the symbol's function cell is void, a
void-functionerror is signaled.This function does not check that the returned object is a legitimate function.
(defun bar (n) (+ n 2)) ⇒ bar (symbol-function 'bar) ⇒ (lambda (n) (+ n 2)) (fset 'baz 'bar) ⇒ bar (symbol-function 'baz) ⇒ bar
If you have never given a symbol any function definition, we say that
that symbol's function cell is void. In other words, the function
cell does not have any Lisp object in it. If you try to call such a symbol
as a function, it signals a void-function error.
Note that void is not the same as nil or the symbol
void. The symbols nil and void are Lisp objects,
and can be stored into a function cell just as any other object can be
(and they can be valid functions if you define them in turn with
defun). A void function cell contains no object whatsoever.
You can test the voidness of a symbol's function definition with
fboundp. After you have given a symbol a function definition, you
can make it void once more using fmakunbound.
This function returns
tif the symbol has an object in its function cell,nilotherwise. It does not check that the object is a legitimate function.
This function makes symbol's function cell void, so that a subsequent attempt to access this cell will cause a
void-functionerror. It returns symbol. (See alsomakunbound, in Void Variables.)(defun foo (x) x) ⇒ foo (foo 1) ⇒1 (fmakunbound 'foo) ⇒ foo (foo 1) error--> Symbol's function definition is void: foo
This function stores definition in the function cell of symbol. The result is definition. Normally definition should be a function or the name of a function, but this is not checked. The argument symbol is an ordinary evaluated argument.
There are three normal uses of this function:
- Copying one symbol's function definition to another—in other words, making an alternate name for a function. (If you think of this as the definition of the new name, you should use
defaliasinstead offset; see Definition of defalias.)- Giving a symbol a function definition that is not a list and therefore cannot be made with
defun. For example, you can usefsetto give a symbols1a function definition which is another symbols2; thens1serves as an alias for whatever definitions2presently has. (Once again usedefaliasinstead offsetif you think of this as the definition ofs1.)- In constructs for defining or altering functions. If
defunwere not a primitive, it could be written in Lisp (as a macro) usingfset.Here are examples of these uses:
;; Savefoo's definition inold-foo. (fset 'old-foo (symbol-function 'foo)) ;; Make the symbolcarthe function definition ofxfirst. ;; (Most likely,defaliaswould be better thanfsethere.) (fset 'xfirst 'car) ⇒ car (xfirst '(1 2 3)) ⇒ 1 (symbol-function 'xfirst) ⇒ car (symbol-function (symbol-function 'xfirst)) ⇒ #<subr car> ;; Define a named keyboard macro. (fset 'kill-two-lines "\^u2\^k") ⇒ "\^u2\^k" ;; Here is a function that alters other functions. (defun copy-function-definition (new old) "Define NEW with the same function definition as OLD." (fset new (symbol-function old)))
fset is sometimes used to save the old definition of a
function before redefining it. That permits the new definition to
invoke the old definition. But it is unmodular and unclean for a Lisp
file to redefine a function defined elsewhere. If you want to modify
a function defined by another package, it is cleaner to use
defadvice (see Advising Functions).
