Pattern matching is a powerful feature in Haskell that allows you to check a value against a pattern. It is a fundamental concept that is used extensively in Haskell for deconstructing data structures, handling different cases, and simplifying code.
Key Concepts
-
Basic Pattern Matching:
- Matching literals and variables.
- Using patterns in function definitions.
-
Pattern Matching with Lists:
- Deconstructing lists.
- Using the cons operator (
:
).
-
Pattern Matching with Tuples:
- Deconstructing tuples.
- Matching nested tuples.
-
Wildcard Patterns:
- Ignoring parts of a pattern with
_
.
- Ignoring parts of a pattern with
-
As-Patterns:
- Capturing the entire value while matching its components.
Basic Pattern Matching
In Haskell, you can use pattern matching in function definitions to handle different cases. Here’s a simple example:
Explanation:
isZero 0 = True
: This pattern matches if the input is0
.isZero _ = False
: The underscore_
is a wildcard pattern that matches any value.
Pattern Matching with Lists
Lists are a fundamental data structure in Haskell, and pattern matching is often used to deconstruct them.
-- Function to get the head of a list head' :: [a] -> a head' [] = error "Empty list" head' (x:_) = x
Explanation:
head' [] = error "Empty list"
: This pattern matches an empty list and throws an error.head' (x:_) = x
: This pattern matches a non-empty list, wherex
is the head and_
is the tail (ignored).
Pattern Matching with Tuples
Tuples are another common data structure, and pattern matching can be used to deconstruct them.
-- Function to add two pairs of integers addPairs :: (Int, Int) -> (Int, Int) -> (Int, Int) addPairs (a, b) (c, d) = (a + c, b + d)
Explanation:
addPairs (a, b) (c, d) = (a + c, b + d)
: This pattern matches two pairs of integers and adds their corresponding elements.
Wildcard Patterns
Wildcard patterns are useful when you want to ignore certain parts of a pattern.
-- Function to check if a list has at least one element hasOneElement :: [a] -> Bool hasOneElement (_:[]) = True hasOneElement _ = False
Explanation:
hasOneElement (_:[]) = True
: This pattern matches a list with exactly one element (ignoring the element itself).hasOneElement _ = False
: This pattern matches any other list.
As-Patterns
As-patterns allow you to capture the entire value while matching its components.
-- Function to duplicate the first element of a list duplicateHead :: [a] -> [a] duplicateHead xs@(x:_) = x : xs duplicateHead [] = []
Explanation:
xs@(x:_) = x : xs
: This pattern matches a non-empty list, captures it asxs
, and constructs a new list with the head duplicated.duplicateHead [] = []
: This pattern matches an empty list and returns an empty list.
Practical Exercises
Exercise 1: Sum of List Elements
Write a function sumList
that calculates the sum of all elements in a list.
Solution Explanation:
sumList [] = 0
: This pattern matches an empty list and returns0
.sumList (x:xs) = x + sumList(xs)
: This pattern matches a non-empty list, adds the headx
to the sum of the tailxs
.
Exercise 2: Length of a List
Write a function lengthList
that calculates the length of a list.
Solution Explanation:
lengthList [] = 0
: This pattern matches an empty list and returns0
.lengthList (_:xs) = 1 + lengthList(xs)
: This pattern matches a non-empty list, adds1
to the length of the tailxs
.
Common Mistakes and Tips
- Forgetting Base Cases: Always include base cases (e.g., empty list) to avoid runtime errors.
- Overlapping Patterns: Ensure patterns are mutually exclusive or ordered correctly to avoid unexpected behavior.
- Using Wildcards Wisely: Use wildcards to ignore irrelevant parts, but avoid overusing them as they can make code less readable.
Conclusion
Pattern matching is a versatile and powerful feature in Haskell that simplifies code by allowing you to deconstruct data structures and handle different cases elegantly. By mastering pattern matching, you can write more concise and readable Haskell code. In the next topic, we will explore Guards, which provide another way to handle different cases in function definitions.
Haskell Programming Course
Module 1: Introduction to Haskell
- What is Haskell?
- Setting Up the Haskell Environment
- Basic Syntax and Hello World
- Haskell REPL (GHCi)