Friday, November 27, 2026

Updates to named-let

Updating named-let: ignored bindings, multi-form inits, and more

named-let is a small Common Lisp library of Scheme-inspired binding macros: named-let, named-lambda, letrec, letrec*, flambda, and a Lisp-1 style define. It also provides an extended let that you can shadow-import in place of the standard one. This post describes a round of changes to the library: a new way to ignore bindings, clarified handling of multi-form bindings, a bug fix, and improvements to documentation and tests.

Ignored bindings with _

Sometimes you want to bind something only for its side effect. In standard Common Lisp you must invent a variable and declare it ignored:

(let ((a 22)
      (dummy1 (format t "foo"))
      (b (sqrt 2))
      (dummy2 (format t "bar")))
  (declare (ignore dummy1 dummy2))
  body)

Now the extended let does this for you. Any binding of the symbol _ is replaced by a fresh uninterned symbol (a gensym), and a single ignore declaration for all such symbols is added at the start of the body:

(let ((a 22)
      (_ (format t "foo"))
      (b (sqrt 2))
      (_ (format t "bar")))
  body)

;; expands to

(let ((a 22)
      (#:ignore-1 (format t "foo"))
      (b (sqrt 2))
      (#:ignore-2 (format t "bar")))
  (declare (ignore #:ignore-1 #:ignore-2))
  body)

A few details worth knowing:

  • The init forms are still evaluated, and in order, so _ is useful for side effects.
  • You can have any number of _ bindings; each gets its own gensym, so there is no “variable occurs more than once” error.
  • The bare forms _ and (_) are accepted and bind to nil.
  • _ is matched by name, not by identity, so it works no matter which package your code is read in.
  • A declare at the start of your own body coexists with the generated one.
  • Because the generated declaration is placed first, the ignored variable can never be referenced from the body; _ is not a variable you can read.

The same convention applies to a named let, because its bindings become the parameters of a named-lambda, and to named-lambda parameters themselves:

(funcall (named-lambda second-arg (_ x _) x) 1 3 2)
;; => 3

(funcall (named-lambda f (a &optional (_ 9)) (+ a 3)) 1)
;; => 4

Multiple forms in a binding

A binding in the extended let may contain more than one init form. The forms are wrapped in a progn and the last value is bound:

(let ((a (format t "binding a") (+ 2 (sqrt x))))
  (+ a b))

;; expands to

(let ((a (progn (format t "binding a") (+ 2 (sqrt x)))))
  (+ a b))

The library already handled this, so no code change was needed, but it is now pinned down with a test and documented. As in standard Lisp, a bare variable y or a one-element binding (z) is bound to nil, and all bindings are made in parallel: init forms do not see the new variables.

How the expansion works

The extended let looks at its first argument. If it is a list (or nil), it is an ordinary binding list; the macro walks the bindings, replaces each _ with a gensym while collecting the gensyms, and emits the underlying let with the bindings, an optional (declare (ignore ...)), and the body. If the first argument is a symbol, it is the name of a named let and the expansion is a funcall of a named-lambda.

As before, the macros are SERIES-aware. Expansions refer to let and funcall in the SERIES package when it is loaded and the symbol is fbound there, and to COMMON-LISP otherwise. That is why an expansion you inspect in an image with SERIES loaded will begin with series::let. Expansion templates use qualified symbols so they resolve to COMMON-LISP symbols regardless of any shadowing in the consumer's package.

A bug fix in named-lambda

Writing more tests for _ turned up two bugs in named-lambda, which also supports _ parameters:

  1. The binding that extracts an optional docstring had a misplaced parenthesis, which made the let* binding spec malformed and caused a compile error in the macro definition.
  2. It recognised _ by comparing with eq against the symbol named-let::_. A _ read in any other package is a different symbol and so was not recognised, leading to a “variable _ occurs more than once in the lambda list” error. It now compares symbol names, exactly as let does.

Docstrings and comments

Every macro in the library now has a docstring. named-lambda and named-let were missing them, and letrec* had copied letrec's text; it now says its bindings are initialised sequentially so later ones can use earlier ones. I also added short comments in the places where the code is not self-explanatory: the gensym-and-declare logic in let, the difference between letrec (simultaneous assignment) and letrec* (sequential assignment), and the _ handling in named-lambda.

Tests

The FiveAM suite grew from 24 to 44 checks. The new tests cover:

  • _ in let: evaluation order of the init forms, the shape of the expansion (distinct uninterned symbols and a single declare), bare _ and (_), all-_ binding lists, and a user declare alongside the generated one.
  • Multi-form bindings, both the expansion and the resulting value and side effects.
  • Parallel binding semantics and a let with only a declare in its body.
  • named-lambda with repeated _ parameters, _ with a default in &optional, and a docstring.

Run them with (asdf:test-system "named-let-test"). One practical note when writing your own: do not use names locked by the implementation (such as loop or count) as the name of a named let.

Documentation

The README was rewritten with a summary table and examples for every macro, including the previously undocumented flambda. Along the way I corrected a few inaccuracies: the clone URL, and the claim that define defines local functions (it is a top-level form that stores the function in the symbol's value cell and defines a macro that funcalls it).

There is also a short section on choosing a name for a named let. When the name just means “go around again”, a short one such as next or l reads well:

(named-let next ((i 0) (acc '()))
  (if (= i 5)
      (nreverse acc)
      (next (1+ i) (cons (* i i) acc))))
;; => (0 1 4 9 16)

You cannot call it loop: COMMON-LISP already binds that symbol as a macro, and implementations such as SBCL lock the package. The same is true of other standard names like count.

Getting it

The code is on GitHub at jrm-code-project/named-let. Clone it into your Quicklisp local-projects directory and load it with (ql:quickload "named-let").


No comments: