liveBook · Manning

Wildcard

As explained in Learn Haskell by Example

A wildcard pattern is a pattern that matches any input without binding that input to an identifier.

A wildcard pattern, written _, matches any input without binding that input to an identifier. Use it when a value is structurally required but its contents are irrelevant. section 3.3.1 section 4.1.2

Ignoring an unused function argument

A recursive line-numbering helper can ignore its first argument when processing an empty list: `go _ [] = []`. The counter is unnecessary in this case, so `_` keeps the pattern match clear without introducing an unused name. section 4.1.2

Providing a catch-all pattern

A filename parser can accept exactly one filename and handle every other list shape with a wildcard: `parseArguments [filePath] = filePath` followed by `parseArguments _ = undefined`. Because `_` always matches, a final wildcard makes pattern matching exhaustive, although this particular fallback still crashes for invalid input. section 3.3.1

Listing 4.3 A function that numbers every line in order
numberAllLines :: [String] -> NumberedLines
numberAllLines lines =
  let go :: Int -> [String] -> NumberedLines    #1
      go _ [] = []                                   #2
      go counter (x : xs) = (Just counter, x) : go (counter + 1) xs   #3
   in go 1 lines     #4

Inspect the complete recursive line-numbering definition to see how a wildcard can hide an irrelevant argument while other patterns retain needed values. section 4.1.2

How Learn Haskell by Example covers it

How deep it goes
Defines wildcard matching, demonstrates discarded bindings, and notes exhaustive final patterns without developing error-safe fallbacks.
Examples
Uses filename-argument parsing and recursive line numbering as concrete wildcard examples.
What you can do after
Apply it via pattern matching to ignore values, simplify unused arguments, and close remaining pattern cases.
How it connects
Connects wildcard patterns to pattern matching, list patterns, recursive functions, and exhaustive matching.

Also explained in

Compare how 8 books explain wildcard →

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.