Tracks
/
Haskell
Haskell
/
Syllabus
/
Pattern Matching Literals
Pa

Pattern Matching Literals in Haskell

1 exercise

About Pattern Matching Literals

While if/else expressions can be used to execute conditional logic, Haskell also has a more powerful way to execute conditional logic: pattern matching. With pattern matching, a value can be tested against one or more patterns. An example of such a pattern is the constant pattern, which matches a value against a constant (e.g. 1 or "hello").

When defining functions, you can define separate function bodies for different patterns. This leads to clean code that is simple and readable. You can pattern match on any data type — numbers, characters, lists, tuples, etc.

For example, a trivial function that takes a whole number (Int) and makes it 1 closer to 0 could be expressed like this:

closerToZero :: Int -> Int
closerToZero 0 = 0
closerToZero 1 = 0

Pattern matching starts to shine when used together with other patterns, for example the variable pattern:

closerToZero :: Int -> Int
closerToZero 0 = 0
closerToZero n = n - 1

The above example treats all inputs other than 0 the same, and would produce incorrect results for negative numbers. This can be solved using conditional patterns, known as guards, which are expressed with the | symbol:

closerToZero :: Int -> Int
closerToZero n
    | n < 0 = n + 1
    | n > 0 = n - 1

In the above examples not all possible inputs have a matching pattern. The compiler will detect this and output a warning. This is a very useful feature of Haskell that helps ensure all possible paths are covered to avoid run-time errors. It is known as exhaustive checking. To solve the warning, you have to handle all cases. Within guards you can use the expression otherwise as syntactic sugar for True to catch all remaining patterns.

closerToZero :: Int -> Int
closerToZero n
    | n < 0 = n + 1
    | n > 0 = n - 1
    | otherwise = 0

Pattern matching will test a value against each pattern from top to bottom, until it finds a matching pattern and executes the logic associated with that pattern. The order of patterns matters!

Edit via GitHub The link opens in a new window or tab