In Chapter 7, you learned the basics of pattern matching, which is used with functions. In this chapter, you’ll learn more about the details of pattern matching.
Basically, pattern matching means matching values with patterns and binding variables with the matches that succeeded.
f: This pattern matches basically anything and binds the f to anything that fits in.
(x:xs): This pattern matches a list with at least one element; the list contains something (which is bounded to x) created (with (:) operator) from something else (bounded to xs).
[]: This pattern matches an empty list and does not bind anything.
_: This pattern matches everything but does not bind anything.
In the expression (x:xs), the x and xs can be considered subpatterns that match parts of a list, matching with everything that respects the types in the type signature of map. In other words, x matches with anything of type a, and xs matches with anything of type [a], in particular with an empty list. Therefore, a list with one element matches (x:xs).
Recognizing values: For example, map’s definition says that when an empty list is the second parameter, the result will be an empty list. In other words, it is chosen as the first branch.
Binding variables to identified values: For example, f, x, and xs from the previous definition are mapped to the arguments of map when called, and the second branch is chosen. Binding can be seen as a side effect of the fact that variables names are used as patterns, as using _ and [] suggests.
Sectioning values into more parts: For example, the (x:xs) expression binds x with the head of a list and xs with the tail of a list.
Even the subpattern can fail, i.e., y:ys. The as-pattern always matches. Another advantage is that it can be faster because in the first version, the y:ys is reconstructed, instead of reusing the value to which it matched.
Success: When a pattern matching process succeeds, the variables are bounded with the arguments.
Fail: When in a pattern matching process an equation fails, then the matching process moves to the next equation, and so on. If all equations fail, then a runtime error occurs.
Divergence: When a pattern matching process diverges , it means that a value needed in the pattern contains an error.
Pattern Matching and Constructors
Not all functions are allowed to be used in pattern matching. In pattern matching you use just constructors —those functions that construct data types.
Here, Zero and Double are constructors for the MyData type. You can use them to pattern match Zero with the 0 value of the Int type and bind a value constructed with Double from MyData with its Int double.
Here, the type constructors for the list are the empty list and the (:) operator.
Note that the previous definition is not actually correct; we used it as an intuitive explanation of the reason why the pattern matching works on lists. In fact, in Haskell, lists are important, and they have a much more complex construction.
Tuples have a similar explanation.
Pattern matching is useful in records. For a comprehensive explanation of records, refer to Chapter 4.
Uses of Pattern Matching
Equations (see the map example)
let expressions
Prelude> y = let (x:_) = map (+8) [5,6] in x * 3Prelude> y39
where clauses
Prelude> y = x * 3 where (x:_) = map (+8) [5,6]Prelude> y39
Lambda abstractions
Prelude> switch = (a,b) -> (b,a)Prelude> switch (5,6)(6,5)
List comprehension
associate :: Eq a => a -> [(a, t)] -> [t]associate character xs = [y | (x,y) <- xs , x == character]Prelude> associate 'D' [('D', 15), ('B', 0), ('D', 35), ('D', 100)][15,35,100]
do blocks
firstLetter = do(x:_) <- getLineputStrLn [x]
Summary
What pattern matching is
How pattern matching and constructors are related
When to use pattern matching
References
- 1.
M. Lipovaca, Learn You a Haskell for Great Good! A Beginner’s Guide (No Starch Press, 2011)
- 2.
B. O’Sullivan, J. Goerzen, and D. B. Stewart, Real-World Haskell: Code You Can Believe In (O’Reilly Media, 2008)
- 3.
T. Sheard and S. P. Jones, “Template Meta-programming for Haskell” in proceedings of the 2002 ACM SIGPLAN workshop on Haskell (ACM, 2002)
- 4.
Case expressions and pattern matching, https://www.cs.auckland.ac.nz/references/haskell/haskell-intro-html/patterns.html