match in F#
An expression that must cover every case; the compiler audits.
// match is an expression: every arm yields a value, all cases covered.
let describe frames =
match frames with
| 0 -> "empty box"
| 1 -> "a single frame"
| n when n < 10 -> sprintf "%d frames, room to grow" n
| _ -> "packed full"
[0; 1; 6; 14] |> List.iter (describe >> printfn "%s")
// Tuples destructure right in the pattern.
let advice =
match ("spring", 12) with
| ("spring", n) when n > 10 -> "add a super"
| (season, _) -> sprintf "hold steady through %s" season
printfn "%s" advice
How it works
- Arms try in order;
whenguards refine a pattern. _is the catch-all that completes the coverage.- Tuples destructure right in the pattern:
("spring", n).
Keywords and builtins used here
letmatchwhenwith
The run, in numbers
- Lines
- 16
- Characters to type
- 491
- Tokens
- 107
- Three-star pace
- 65 tpm
At the three-star pace of 65 tokens a minute, this run takes about 99 seconds.
Step 1 of 3 in Pattern matching, step 7 of 27 in Language basics.