I don\'t know if you would call it the canonical formulation, but to bind a local function I am advised by the GNU manual to use \'flet\':
(defun adder-with-flet
You don't have to use flet
if you do not want to. You place a function in the function cell of a local symbol defined using let
as in the following example:
(let ((ALocalSymbol))
(fset 'ALocalSymbol (lambda (x) (* 2 x)))
(ALocalSymbol 4)
)
Evaluating this will return 8. Do notice the quote in front of ALocalSymbol
in (let ((ALocalSymbol))...)
. While setq
quotes symbols, fset
does not.
flet
is a syntactic sugar of sorts. Using a plain-old let
to define nil-valued symbols, allows you to choose which "cell" of a symbol to set. You could use setq
to set the symbol's value cell or fset
to set the function cell.
Hope this helps,
Pablo