typestar

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

  1. "Clover" :: spring makes a new list sharing the old tail.
  2. [1 .. 2 .. 9] is a stepped range literal.
  3. first :: rest destructures head and tail in a match.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 3 in Lists, step 10 of 27 in Language basics.

← Previous Next →

Lists and cons in other languages