Let Bindings, Scope & Immutability
Bindings are immutable — let gives a name to a value; it is not a variable assignment.
5.1 Top-level bindings
Bindings are immutable — let gives a name to a value; it is not a variable assignment.
let pi = 3.14159
let radius = 2.0
let area = pi *. radius *. radius5.2 Local bindings: let ... in
let x = e1 in e2 binds x only inside e2. The whole thing is an expression.
let area_of_circle r =
let pi = 3.14159 in
let r2 = r *. r in
pi *. r25.3 Shadowing
Closures that captured the old x still see 10.
let x = 10
let x = x + 1 (* new binding x = 11; the old one is shadowed, not mutated *)5.4 Simultaneous bindings
let a = 1 and b = 2 (* both defined at once; b can't refer to a *)
let x, y = (3, 4) (* destructuring *)5.5 Ignoring values
Discarding a non-unit value with ; produces a warning — use ignore.
let _ = some_function () (* discard *)
let () = print_endline "side" (* assert result is unit *)
ignore (List.length [1;2]) (* explicit discard *)