Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Bindings and scope

Immutable and mutable locals

let binds an ordinary lexical name. A let binding cannot be reassigned.

(let greeting "hello")
(let answer (+ 40 2))

mut declares a mutable local. Update it with alter!, +=, ++, and the other mutation helpers:

(mut total 0)
(alter! total (+ total 10))
(+= total 20)
(++ total)
total ; 31

alter! takes a value, not a callback. It is equivalent in spirit to assignment after the new expression has been evaluated.

Destructuring

Tuple and vector patterns can bind components directly:

(let {name age} {"Ada" 37})
(let [first second] [10 20])

Vector destructuring has the usual shape constraint: a fixed pattern such as [a b] requires the vector to contain those elements. The analyser may warn when it cannot prove that fact. A final . rest binding works in both let and lambda vector patterns:

(let [a b . rest] [10 20 30 40])
a    ; 10
b    ; 20
rest ; [30 40]

(let get-head (lambda ([x . xs]) x))

The names before . bind fixed positions. rest is the same tail window as (cdr fixed-position-count vector): it begins after those positions without initially copying the remaining values. It materializes independently if you mutate it. The rest pattern must be final and can bind one name only.

Scope

Names are lexical. A binding introduced by block is not visible after that block:

(let label
  (if true
      (block (let value "yes") value)
      (block (let value "no") value)))

do sequences values but does not make a lexical scope. This distinction matters in branches and loops: prefer block for branch-local temporary bindings.

Reference cells

&mut, &get, and &alter! are macros for an explicit mutable cell. They are useful when a closure must update state that outlives a single local scope:

(&mut counter 0)
(let next!
  (lambda ()
    (&alter! counter (+ (&get counter) 1))))

(next!) ; 1
(next!) ; 2

They preserve the compiler’s normal mutation analysis; they are source-level convenience forms, not an untyped escape hatch.