let bindings in F#
Immutable by default; mutable is a separate, spelled-out choice.
// let binds immutably; mutable is a separate, spelled-out choice.
let queenYear = 2024
let colonyName = "Willow"
// Inside a scope, shadowing rebinds the name to a new value.
let describeFrames frames =
let frames = frames + 2 // shadowed: counts the super frames too
sprintf "%d frames counting supers" frames
printfn "%s: %s (queen %d)" colonyName (describeFrames 8) queenYear
// The mutable escape hatch updates with <- and stays rare.
let mutable smokerFuel = 100
smokerFuel <- smokerFuel - 35
printfn "Smoker fuel at %d%%" smokerFuel
How it works
- Top-level
letbinds once for the whole script. - Inside a scope, a new
letshadows the outer name. let mutableplus<-is the explicit escape hatch.
Keywords and builtins used here
let
The run, in numbers
- Lines
- 14
- Characters to type
- 542
- Tokens
- 54
- Three-star pace
- 70 tpm
At the three-star pace of 70 tokens a minute, this run takes about 46 seconds.
Step 2 of 3 in Values, step 2 of 27 in Language basics.