LAMBDA
expression or that you need to use more than once. Both have the same basic form, which looks like this:
(flet (
and like this:
(labels (
where each
(
The difference between FLET
and LABELS
is that the names of the functions defined with FLET
can be used only in the body of the FLET
, while the names introduced by LABELS
can be used immediately, including in the bodies of the functions defined by the LABELS
. Thus, LABELS
can define recursive functions, while FLET
can't. It might seem limiting that FLET
can't be used to define recursive functions, but Common Lisp provides both FLET
and LABELS
because sometimes it's useful to be able to write local functions that can call another function of the same name, either a globally defined function or a local function from an enclosing scope.
Within the body of a FLET
or LABELS
, you can use the names of the functions defined just like any other function, including with the FUNCTION
special operator. Since you can use FUNCTION
to get the function object representing a function defined with FLET
or LABELS
, and since a FLET
or LABELS
can be in the scope of other binding forms such as LET
s, these functions can be closures.
Because the local functions can refer to variables from the enclosing scope, they can often be written to take fewer parameters than the equivalent helper functions. This is particularly handy when you need to pass a function that takes a single argument as a functional parameter. For example, in the following function, which you'll see again in Chapter 25, the FLET
ed function, count-version
, takes a single argument, as required by walk-directory
, but can also use the variable versions
, introduced by the enclosing LET
:
(defun count-versions (dir)
(let ((versions (mapcar #'(lambda (x) (cons x 0)) '(2 3 4))))
(flet ((count-version (file)
(incf (cdr (assoc (major-version (read-id3 file)) versions)))))
(walk-directory dir #'count-version :test #'mp3-p))
versions))
This function could also be written using an anonymous function in the place of the FLET
ed count-version
, but giving the function a meaningful name makes it a bit easier to read.
And when a helper function needs to recurse, an anonymous function just won't do.[208] When you don't want to define a recursive helper function as a global function, you can use LABELS
. For example, the following function, collect- leaves
, uses the recursive helper function walk
to walk a tree and gather all the atoms in the tree into a list, which collect-leaves
then returns (after reversing it):
(defun collect-leaves (tree)
(let ((leaves ()))
(labels ((walk (tree)
(cond
((null tree))
((atom tree) (push tree leaves))
(t (walk (car tree))
(walk (cdr tree))))))
(walk tree))
(nreverse leaves)))
Notice again how, within the walk
function, you can refer to the variable, leaves
, introduced by the enclosing LET
.
FLET
and LABELS
are also useful operations to use in macro expansions—a macro can expand into code that contains a FLET
or LABELS
to create functions that can be used within the body of the macro. This technique can be used either to introduce functions that the user of the macro will call or simply as a way of organizing the code generated by the macro. This, for instance, is how a function such as CALL-NEXT-METHOD
, which can be used only within a method definition, might be defined.
A near relative to FLET
and LABELS
is the special operator MACROLET
, which you can use to define local macros. Local macros work just like global macros defined with DEFMACRO
except without cluttering the global namespace. When a MACROLET
form is evaluated, the body forms are evaluated with the local macro definitions in effect and possibly shadowing global function and macro definitions or local definitions from enclosing forms. Like FLET
and LABELS
, MACROLET
can be used directly, but it's also a handy target for macro- generated code—by wrapping some user-supplied code in a MACROLET
, a macro can provide constructs that can be used only within that code or can shadow a globally defined macro. You'll see an example of this latter use of MACROLET
in Chapter 31.
Finally, one last macro-defining special operator is SYMBOL-MACROLET
, which defines a special kind of macro called, appropriately enough, a WITH-SLOTS
and WITH- ACCESSORS
are able to define 'variables' that access the state of a particular object under the covers. For instance, the following WITH-SLOTS
form:
(with-slots (x y z) foo (list x y z)))
might expand into this code that uses SYMBOL-MACROLET
:
(let ((#:g149 foo))
(symbol-macrolet
((x (slot-value #:g149 'x))
(y (slot-value #:g149 'y))
(z (slot-value #:g149 'z)))
(list x y z)))
When the expression (list x y z)
is evaluated, the symbols x
, y
, and z
will be replaced with their expansions, such as (slot-value #:g149 'x)
.[209]
Symbol macros are most often local, defined with SYMBOL-MACROLET
, but Common Lisp also provides a macro DEFINE-SYMBOL-MACRO
that defines