Closures(v2 · en)
Concept
A closure = a function value + a captured lexical environment. An fn remembers, at its definition site, the bindings it can see; wherever the function value travels afterwards, it still reads those same binding cells — not same-named ones at the call site. What is captured is the binding, not a copy of the value. That is precisely the contrast with an ordinary function: an ordinary function's output is determined by its input alone, while a closure's behavior also depends on the captured environment.
(defun make-adder [k] (fn [x] (+ x k)))
(println ((make-adder 4) 3))
Expected stdout:
7
Captured bindings are mutable: set! updates the captured cell, and the closure thereby gains memory. Each factory call produces independent parameter bindings — two adders do not share k, and no number of calls accumulates anything:
(defun make-adder [k] (fn [x] (+ x k)))
(def add4 (make-adder 4))
(def add10 (make-adder 10))
(println (add4 1) (add10 1))
(println (add4 1) (add10 1))
Expected stdout:
5 11
5 11
k is never touched by set!, so the captured bindings keep their initial values; each adder reads its own 4 and 10. Sharing presupposes mutability — see Shared State.
Revision notes (v2)
The v1 body demonstrated a single capture only; this version adds the "adder family" example, writing "each call captures independently" and "sharing presupposes mutability" into the body and aligning them with the sharing/isolation observations of Shared State. Relations and experiment attachments are unchanged.
Going further
The Scheme lambda paper established closures as first-class citizens: the environment travels with the function. JavaScript's var-in-a-loop trap and Python's late-binding closure pitfall are accident reports from misreading "captures the binding, not the value". Zio's choice matches Scheme: capture bindings, keep behavior predictable; for isolation, call the factory again. The experiment Closure sharing and isolation runs both paths side by side.