typestar

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

  1. Top-level let binds once; let ... in scopes to one expression.
  2. ref 0 makes a mutable cell; := writes and ! reads.
  3. The scoped binding vanishes after its expression ends.

Keywords and builtins used here

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.

Type this snippet

Step 2 of 3 in Values, step 2 of 29 in Language basics.

← Previous Next →

let bindings in other languages