4.3.2 Function Bindings

These forms make let-like bindings to functions instead of variables.

Macro: cl-flet (bindings…) forms…

This form establishes let-style bindings for functions rather than values. Each binding must be a list of one of two forms: either (name expr) or (name arglist body…). The name is the name of the function, expr is an expression which returns the function value to which the corresponding name should be bound, and arglist and body are the argument list and the body of the function to bind to name. Within forms, any reference to the function name uses the local definition provided by bindings instead of the global one.

A “reference” to a function name is either a call to that function, or a use of its name quoted by function to be passed on to, say, mapcar.

The bindings are lexical in scope. This means that all references to the named functions must appear physically within forms.

Functions defined by cl-flet may use the full Common Lisp argument notation supported by cl-defun; also, the function body is enclosed in an implicit block as if by cl-defun. See Program Structure.

Note that the cl.el version of this macro behaves slightly differently. In particular, its binding is dynamic rather than lexical. See Obsolete Macros.

Macro: cl-labels (bindings…) forms…

The cl-labels form is like cl-flet, except that the function bindings can be recursive. The scoping is lexical, but you can only capture functions in closures if lexical-binding is t. See Closures in GNU Emacs Lisp Reference Manual, and Using Lexical Binding in GNU Emacs Lisp Reference Manual.

Lexical scoping means that all references to the named functions must appear physically within the body of the cl-labels form. References may appear both in the body forms of cl-labels itself, and in the bodies of the functions themselves. Thus, cl-labels can define local recursive functions, or mutually-recursive sets of functions.

Note that the cl.el version of this macro behaves slightly differently. See Obsolete Macros.