Destructuring Values with Pattern Matching

In the last section, as you worked with lists, you learned how the shape of a list was reflected in the way you worked with it using functions like (:), head and tail. The idea that the shape of a data structure can be reflected in how we write code to use that data structure is a powerful one, and it turns out to have much more broad-reaching applicability than just lists.

Using pattern matching lets you write powerful expressions that match parts of a value based on its shape. You’ll learn several ways to use pattern matching in the remainder of this chapter. Let’s start by looking at how you can use pattern matching with functions, case statements, and let bindings.

In its simplest form, a pattern ...

Get Effective Haskell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.