0% found this document useful (0 votes)
3 views10 pages

Haskell Pattern Matching Explained

The document discusses pattern matching in Haskell, explaining its outcomes: success, failure, and divergence, and how it can be used to define functions with multiple definitions based on different patterns. It emphasizes the readability and expressiveness of code achieved through pattern matching on various data types. Additionally, it covers the use of 'where' bindings for improving code clarity and efficiency by avoiding redundant calculations.

Uploaded by

Aditya Chaudhary
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Haskell Pattern Matching Explained

The document discusses pattern matching in Haskell, explaining its outcomes: success, failure, and divergence, and how it can be used to define functions with multiple definitions based on different patterns. It emphasizes the readability and expressiveness of code achieved through pattern matching on various data types. Additionally, it covers the use of 'where' bindings for improving code clarity and efficiency by avoiding redundant calculations.

Uploaded by

Aditya Chaudhary
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Pattern matching

• Pattern matching can either fail, succeed or diverge.


• A successful match binds the formal parameters in the pattern.
• Divergence occurs when a value needed by the pattern contains an error (_|_).
• The matching process itself occurs "top-down, left-to-right."
• Failure of a pattern anywhere in one equation results in failure of the whole
equation, and the next equation is then tried.
• If all equations fail, the value of the function application is _|_, and results in a run-
time error.
• When defining functions, you can define separate function bodies for
different patterns. You can pattern match on any data type – numbers,
characters, lists, tuples, etc. This leads to a very expressive code that is also
simple and readable.

S6CSE, Department of CSE, Amritapuri


Defining functions in Haskell
• We are not restricted to having single line definitions for functions.
• We can use multiple definitions combined with implicit pattern matching.
• Consider the function:

Here, the first equation is used if the second argument to power is 0. If the
second argument is not 0, the first definition does not ``match'', so we
proceed to the second definition. When multiple definitions are provided,
they are scanned in order from top to bottom.
S6CSE, Department of CSE, Amritapuri
Defining functions in Haskell
• Another example of a function specified via multiple definitions, using
pattern matching. We can use multiple definitions combined with
implicit pattern matching.
• Consider the function:

Here, the first two lines explicitly describe two interesting patterns, and
the last line catches all combinations that do not match.

S6CSE, Department of CSE, Amritapuri


Pattern matching
• Consider the example :-

This function isValidName behavior in 2 scenarios:


• receiving an empty string as the parameter, then returning a message about
an invalid name;
• receiving any string value — observe that when typed the receiving
parameter as a String. If tried to send a Number instead of a String, it would
throw an error — , then returning a message including the valid name.

S6CSE, Department of CSE, Amritapuri


Guarded Equations
• Note - all variables used in patterns are substituted independently--
we cannot directly ``match'' arguments in the pattern by using the
same variable for two arguments to implicitly check that they are the
same.
• For instance, the following will not work.

• Instead, we must write

S6CSE, Department of CSE, Amritapuri


where ?
• Consider

• Notice that we repeat weight / height ^ 2 three times.


• It would be ideal if we could calculate it once, bind it to a name and
then use that name instead of the expression.

S6CSE, Department of CSE, Amritapuri


where ?
• To make it more readable by giving names to things and can make our
programs faster since stuff like bmi variable here is calculated only
once.

S6CSE, Department of CSE, Amritapuri


where ?
• where bindings aren't shared across function bodies of different
patterns. If one wants several patterns of one function to access some
shared name, it has to be defined it globally.
• Also use where bindings to pattern match! The previous can again be
modified as:

S6CSE, Department of CSE, Amritapuri


where?
• Consider a function where we get a first and a last name and give
someone back their initials.

S6CSE, Department of CSE, Amritapuri


S6CSE, Department of CSE, Amritapuri

You might also like