Lists and cons in F#
Immutable, singly linked: :: builds, @ appends, patterns split.
// Lists are immutable and singly linked; :: builds, @ appends.
let spring = ["Willow"; "Bramble"]
let summer = "Clover" :: spring
let yard = summer @ ["Thistle"]
printfn "%A" yard
printfn "First: %s, count: %d" (List.head yard) (List.length yard)
// Ranges and pattern-matched heads and tails.
let frames = [1 .. 2 .. 9]
printfn "Odd frames: %A" frames
match yard with
| first :: rest -> printfn "%s leads %d others" first (List.length rest)
| [] -> printfn "No hives at all"
How it works
"Clover" :: springmakes a new list sharing the old tail.[1 .. 2 .. 9]is a stepped range literal.first :: restdestructures head and tail in a match.
Keywords and builtins used here
letmatchwith
The run, in numbers
- Lines
- 15
- Characters to type
- 479
- Tokens
- 94
- Three-star pace
- 65 tpm
At the three-star pace of 65 tokens a minute, this run takes about 87 seconds.
Step 1 of 3 in Lists, step 10 of 27 in Language basics.