typestar

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

  1. Top-level let binds once for the whole script.
  2. Inside a scope, a new let shadows the outer name.
  3. let mutable plus <- is the explicit escape hatch.

Keywords and builtins used here

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.

Type this snippet

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

← Previous Next →

let bindings in other languages