liveBook · Manning

Pattern matching

As explained in Learn Haskell by Example

Pattern matching is a language mechanism that selects computation branches by comparing values against structural patterns.

Pattern matching selects a function definition from the shape of its input. In Haskell, it deconstructs data directly in a function’s arguments, so recursive list code can distinguish the empty list from a nonempty list without loops. section 2.4.1 section 2.6

Lists are built with two constructors: [] for an empty list and (:) for an element prepended to a tail. A complete list function normally supplies one pattern for each constructor. Matching only [] leaves other inputs undefined and produces a nonexhaustive-pattern error. section 2.4.1

Listing 2.7 Function displaying the usual patterns for pattern matching on lists
listLength [] = 0     #1
listLength (x:xs) = ...     #2

These two equations give separate behavior for an empty list and for a head x with remaining elements xs. The second equation can recurse on xs until it reaches []. section 2.4.1

Use patterns to make input cases explicit:

  • Match [] for the base case of a recursive list function. section 2.4.1
  • Match (x : xs) to bind a nonempty list’s first element and remaining list. section 2.4.1
  • Match [x], equivalent to x : [], when exactly one list element is valid. section 3.3.1
  • Use _ when a fallback case should match but its input is intentionally discarded; placing it last makes the definition exhaustive. section 3.3.1

Transforming every character

A Caesar cipher rebuilds a string rather than changing it. The [] case returns an empty string; the (x : xs) case transforms x, prepends it to the result, and recursively transforms xs. This same shape supports many list transformations. section 2.5

Patterns handle structural cases, while guards provide case distinctions from Boolean predicates. A catch-all pattern can prevent a match failure, but returning undefined for invalid input still causes a crash; error values are introduced with Maybe. section 2.6 section 3.3.1

How Learn Haskell by Example covers it

How deep it goes
Explains list constructors, recursive equations, exhaustiveness, single-element and wildcard patterns; defers error values to Maybe.
Examples
Demonstrates listLength, indexOf, a Caesar cipher, and parsing a single filename argument.
What it assumes
Introduces pattern matching from basic list structure while assuming only the need to replace loops with recursion.
What you can do after
Build recursive Haskell functions that deconstruct lists, cover structural cases, and discard unused fallback input.
How it connects
Connects pattern matching with recursion, lists, immutable transformations, guards, and Maybe.

Also explained in

Compare how 12 books explain pattern matching →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.