liveBook · Manning

List

As explained in F# in Action: A Revised Edition of Get Programming with F#

In F#, a list is an immutable ordered collection whose elements can be processed as a sequence.

An F# list is an immutable, ordered collection. Write items between square brackets, separated by semicolons: [ 1; 2; 3 ]. Its type is generic, so a collection of results can be written as Result list or List<Result>. Unlike .NET's generic List, called ResizeArray in F#, a list cannot have items added, removed, or replaced after creation. section 7.2 section 7.2.1

Lists fit naturally into transformation pipelines. Rather than updating a summary collection with loops and mutable state, filter the items of interest, group or count them, and select a result. Each stage produces a new result set, making the pipeline readable, repeatable, and easy to extend with another transformation. section 7.2.1

Find the team with the most away wins

Given a list of match results, first keep away wins, then count them by away team, then choose the largest count: results |> List.filter isAwayWin |> List.countBy (fun result -> result.AwayTeam) |> List.maxBy (fun (team, count) -> count) section 7.2.1

Listing 7.3 A declarative query implementation
let isAwayWin result = result.AwayGoals > result.HomeGoals
let wonAwayTheMost =
    results
    |> List.filter isAwayWin                           #1
    |> List.countBy (fun result -> result.AwayTeam)    #2
    |> List.maxBy (fun (team, count) -> count)         #3

Use this compact pipeline as a reference for replacing a mutable, nested-loop query with separate list transformations. section 7.2.1

Use lists with a few practical constraints in mind:

  • Prepend a single item efficiently with ::; use @ or List.append carefully when repeatedly appending lists in a tight loop. section 7.2.2 section 7.2.1
  • Inspect partial pipelines in FSI to see intermediate lists, and use editor type hints to spot incompatible pipeline stages. section 7.2.2
  • Pattern match when list shape matters: [] handles empty input, :: extracts a head and remainder, and fixed patterns can test lengths safely. section 8.2.4
  • Model a variable number of related values with a list field, such as Address list; use separate record fields when roles are distinct. section 8.4.3

Start with List for many everyday collections. Lists are eager and immutable, support indexing, and can be consumed where IEnumerable is expected. Choose an array when performance or C# consumption matters more, or a sequence when laziness is needed. section 7.3.9

How F# in Action: A Revised Edition of Get Programming with F# covers it

How deep it goes
Defines syntax, immutability, pipeline operations, debugging, performance cautions, pattern matching, and collection trade-offs.
Examples
Uses soccer results for an away-win query and loan requests for list-pattern matching.
What you can do after
Apply it via List functions, FSI inspection, and pattern matching to query and classify immutable collections.
How it connects
Connects lists to higher-order functions, pipelines, pattern matching, records, arrays, sequences, and IEnumerable.

Also explained in

Compare how 21 books explain list →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of F# in Action: A Revised Edition of Get Programming with F#. The section links point to the parts of the book it draws on.