Cl-flet, Cl-letf, And Cl-labels Explained

A few weeks ago, Bozhidar Batsov had a splendid post on cl-flet, cl-letf, and cl-labels. I didn’t get a chance to write about it then but his post is very useful for understanding these macros and how they differ.

The macros are the replacements for the now deprecated flet, which, in turn, was imported from Common Lisp with the added feature of having dynamic scope. As Batsov points out, making the scope dynamic conflates the notion of local and dynamic scope, which can be a bit confusing. This behavior is preserved by the cl-letf macro for those who need it.

The cl-flet macro is just like the flet macro from Common Lisp. It defines a local function whose definition is available only from within its scope. The salient fact is that its definition is not available from within the function definition so it can’t be called recursively.

The cl-labels macro is like labels from Common Lisp. It’s similar to cl-flet except that its definition is available from within the function definition so it is recursive.

Finally, the cl-letf macro largely replicates the behavior of the old (Elisp) flet macro with the addition that it can dynamically replace the definition of any setf-able definition. This can complicate its use a bit. See Batsov’s post for the details.

If you’re like me, you don’t use any of these macros that often so it’s easy to forget the details. For that reason it’s worthwhile bookmarking Batsov’s post so that you can refresh your memory whenever you need to.

This entry was posted in General and tagged , . Bookmark the permalink.