let bindings in OCaml
Immutable by default; refs are the spelled-out mutable cells.
(* let binds immutably; refs are the spelled-out mutable cells. *)
let species = "hermit crab"
let count = 12
(* let ... in scopes a binding to one expression. *)
let doubled =
let next_survey = count * 2 in
next_survey - 3
let tally = ref 0
let () =
Printf.printf "%s: %d seen, %d projected\n" species count doubled;
tally := !tally + count;
Printf.printf "Running tally: %d\n" !tally
How it works
- Top-level
letbinds once;let ... inscopes to one expression. ref 0makes a mutable cell;:=writes and!reads.- The scoped binding vanishes after its expression ends.
Keywords and builtins used here
inlet
The run, in numbers
- Lines
- 15
- Characters to type
- 388
- Tokens
- 64
- Three-star pace
- 65 tpm
At the three-star pace of 65 tokens a minute, this run takes about 59 seconds.
Step 2 of 3 in Values, step 2 of 29 in Language basics.