Skip to main content
almarefa.net

Back to all posts

How to Use Pattern Matching In Haskell?

Published on
5 min read
How to Use Pattern Matching In Haskell? image

Best Haskell Programming Books to Buy in October 2025

1 Effective Haskell: Solving Real-World Problems with Strongly Typed Functional Programming

Effective Haskell: Solving Real-World Problems with Strongly Typed Functional Programming

BUY & SAVE
$55.05 $57.95
Save 5%
Effective Haskell: Solving Real-World Problems with Strongly Typed Functional Programming
2 Programming in Haskell

Programming in Haskell

BUY & SAVE
$42.99 $47.00
Save 9%
Programming in Haskell
3 Learn Physics with Functional Programming: A Hands-on Guide to Exploring Physics with Haskell

Learn Physics with Functional Programming: A Hands-on Guide to Exploring Physics with Haskell

BUY & SAVE
$49.99
Learn Physics with Functional Programming: A Hands-on Guide to Exploring Physics with Haskell
4 Learn You a Haskell for Great Good!: A Beginner's Guide

Learn You a Haskell for Great Good!: A Beginner's Guide

BUY & SAVE
$35.00 $44.95
Save 22%
Learn You a Haskell for Great Good!: A Beginner's Guide
5 Real World Haskell

Real World Haskell

BUY & SAVE
$24.40 $49.99
Save 51%
Real World Haskell
6 Soar with Haskell: The ultimate beginners' guide to mastering functional programming from the ground up

Soar with Haskell: The ultimate beginners' guide to mastering functional programming from the ground up

BUY & SAVE
$45.99
Soar with Haskell: The ultimate beginners' guide to mastering functional programming from the ground up
7 Parallel and Concurrent Programming in Haskell: Techniques for Multicore and Multithreaded Programming

Parallel and Concurrent Programming in Haskell: Techniques for Multicore and Multithreaded Programming

BUY & SAVE
$25.83 $44.99
Save 43%
Parallel and Concurrent Programming in Haskell: Techniques for Multicore and Multithreaded Programming
8 Haskell in Depth

Haskell in Depth

BUY & SAVE
$57.13 $59.99
Save 5%
Haskell in Depth
+
ONE MORE?

Pattern matching is a powerful feature in Haskell that allows you to destructure and extract information from data structures. It allows you to define functions and expressions based on different patterns that the input can match. Here's a brief explanation of how to use pattern matching in Haskell:

  1. Function Definitions: When defining functions, you can use pattern matching in the function definition. Each pattern corresponds to a specific input case. For example:

myFunction :: Int -> String myFunction 0 = "Zero" myFunction 1 = "One" myFunction x = "Other"

In this example, if the input is 0, it will return "Zero". If the input is 1, it will return "One". And for any other input, it will return "Other".

  1. Function Arguments: Pattern matching can also be used to bind values to function arguments. For instance:

getFirst :: (a, b) -> a getFirst (x, y) = x

Here, the function getFirst takes a tuple as input and extracts the first element by pattern matching on (x, y). It assigns x as the first element of the tuple.

  1. Lists and Recursion: Pattern matching is often used with lists and recursion. You can match specific elements or patterns within a list. For example:

mySum :: [Int] -> Int mySum [] = 0 mySum (x:xs) = x + mySum xs

In this example, mySum recursively sums up all the elements of a list. When the list is empty, it returns 0. Otherwise, it matches the input as (x:xs), where x is the head and xs is the tail of the list. The function then adds x to the sum of the remaining elements.

These are just a few examples of how pattern matching can be used in Haskell. It is a powerful tool for working with complex data structures, allowing you to define behavior based on different patterns and cases.

How to match against empty lists in Haskell?

In Haskell, you can match against empty lists (also known as the empty case) using pattern matching or guards. Here are a few examples:

  1. Using pattern matching:

func [] = "Empty list" func (x:xs) = "Non-empty list"

In this example, if the argument to func is an empty list, it will match the pattern [], and the function will return "Empty list". If the argument is a non-empty list, it will match the pattern (x:xs) and return "Non-empty list".

  1. Using guards:

func lst | null lst = "Empty list" | otherwise = "Non-empty list"

In this example, the function func checks if the list lst is empty using the null function. If lst is empty, it will evaluate the first guard (null lst) and return "Empty list". Otherwise, it will evaluate the second guard (otherwise) and return "Non-empty list".

These are just a few ways to match against empty lists in Haskell. Depending on your specific use case, you may choose a different approach.

How to match multiple patterns in Haskell?

To match multiple patterns in Haskell, you can use pattern guards, pattern synonyms, or the case expression.

  1. Pattern guards: You can use pattern guards by adding additional conditions to your function definition, using the | symbol. Each condition corresponds to a pattern, and when all conditions are met, the corresponding code block is executed. Here's an example:

myFunc :: Int -> String myFunc x | x < 0 = "Negative" | x == 0 = "Zero" | otherwise = "Positive"

  1. Pattern synonyms: Pattern synonyms allow you to define additional patterns for existing data types. You can define multiple patterns and use them interchangeably with the original patterns. Here's an example:

pattern Empty = [] pattern Singleton a = [a] pattern Pair a b = (a, b)

myFunc :: [Int] -> String myFunc Empty = "Empty list" myFunc (Singleton x) = "Singleton list" myFunc (Pair x y) = "Pair of elements" myFunc _ = "Other"

  1. Case expressions: The case expression allows you to pattern match on an expression and execute different code blocks based on the matched pattern. Here's an example:

myFunc :: Int -> String myFunc x = case x of 0 -> "Zero" 1 -> "One" 2 -> "Two" _ -> "Other"

In this example, the code block after each pattern, separated by ->, will be executed based on the matched pattern.

These are some ways to match multiple patterns in Haskell. Choose the one that best suits your needs and style of programming.

What are constructors in pattern matching?

In pattern matching, constructors are symbols or data types that are used to define the structure of a data type. They are used to construct values of that data type.

For example, consider a simple data type called List defined as:

data List a = Empty | Cons a (List a)

In this definition, List is the data type, and Empty and Cons are the constructors. Empty represents an empty list, and Cons represents a list with a value (a) and a tail, which is also a List a.

Constructors can be used in pattern matching to destructure and match against values of the defined data type. For example, to define a function to get the head of a List, we can use pattern matching with the Cons constructor:

head :: List a -> a head (Cons x _) = x

Here, the pattern (Cons x _) matches against a List value constructed with the Cons constructor. The first element of the Cons constructor is bound to x, which represents the head of the list. The underscore _ is used to match the tail of the list, but it is not used in the function body.