Arrays and seqs in F#
Arrays index and mutate; seqs are lazy views over anything.
// Arrays are mutable and indexed; seqs are lazy views over anything.
let readings = [| 33.1; 32.7; 32.9; 31.8 |]
readings.[0] <- 33.4
printfn "First reading now %.1f" readings.[0]
printfn "Array average: %.2f" (Array.average readings)
// A seq computes on demand; the pipeline decides how much to pull.
let cooling =
readings
|> Seq.pairwise
|> Seq.filter (fun (a, b) -> b < a)
|> Seq.length
printfn "Cooling steps: %d" cooling
printfn "As a list: %A" (readings |> Array.toList |> List.truncate 2)
How it works
.[0] <-writes in place — arrays are the mutable collection.Seq.pairwisewalks neighbors without materializing.- Conversions like
Array.toListcross between the families.
Keywords and builtins used here
funlet
The run, in numbers
- Lines
- 14
- Characters to type
- 499
- Tokens
- 104
- Three-star pace
- 65 tpm
At the three-star pace of 65 tokens a minute, this run takes about 96 seconds.
Step 3 of 3 in Collections, step 18 of 27 in Language basics.