Bindings and Scope(v1 · en)
Concept
Bindings give values names. def defines a name in the current scope; let establishes a bounded local binding — when the let body exits, the name and its value disappear together. set! updates the nearest existing binding and never crosses a scope boundary.
(def greeting "hello")
(def base 3)
(println greeting)
(println (let ((x 2) (y base)) (* x y)))
(let ((greeting "hi")) (set! greeting "modified"))
(println greeting)
Expected stdout:
hello
6
hello
The third line's set! rewrites the local binding the let created; after the body exits, the outer greeting is untouched. Scope is lexical: a name resolves in the textual environment where it is defined, not where it is called. An inner binding with the same name shadows the outer one — but only within its own text.
(def who "top")
(defun greet [] (str "hello " who))
(println (let [who "inner"] who))
(println (greet))
Expected stdout:
inner
hello top
greet's body reads who where it was defined — the global "top"; the shadowing let at the call site does not leak in. Closure capture is this same rule taken to its conclusion — see Closures.
Design notes
Lexical scope is "text as contract" made concrete: a name's meaning is fixed by the static structure of the text it sits in, so a reader can determine it without running the program. Dynamic scope (names following the call chain) turns every function call into an implicit parameter channel and was abandoned by the Lisp mainstream long ago; Zio keeps the lexical rule and hands explicit cross-layer passing to parameters, bindings and environments (see Modules and Capability Boundaries). Both let and let* currently bind in sequence — each initializer sees the earlier names. That is the implemented behavior; do not write it up as parallel binding.
Going further
The division of labor between binding and scope is a classic Lisp lesson: Scheme composes all naming behavior from a minimal set of forms (lambda, let, set!), and Clojure layers Var-versus-local distinctions on top. Zio's trio def / let / set! is equally minimal: define, bound, update — no fourth implicit channel. When mutation (set!) meets closures, "shared state" emerges — the topic of Shared State.