<p>Pattern matching is one of those features of Haskell that immediately got me interested as it reduces amount of branching inside of functions I write. Basic idea is that if value constructors are for making data, pattern matching is for taking it apart.</p>
<p>First example is a function that takes a <code>Bool</code> and returns a respective <code>String</code>:</p>
<pre><code>boolToString :: Bool -&gt; String
boolToString n =
    if n
        then &quot;True&quot;
        else &quot;False&quot;</code></pre>
<p>Nothing too fancy, just an <code>if</code> expression inside a function. We can move that <code>if</code> out of there though and define exactly same functionality, but with patterns:</p>
<pre><code>boolToString :: Bool -&gt; String
boolToString True =
    &quot;True&quot;

boolToString False =
    &quot;False&quot;</code></pre>
<p>There’s one definition for <code>boolToString</code>, but two different patterns used.</p>
<p>Second example is bit more complex, this time we have <code>Maybe Int</code> that is being turned into <code>String</code>. <code>Maybe</code> has two value constructors <code>Nothing</code> and <code>Just a</code>. We have two cases for <code>Just</code>, specific one for when it’s <code>Just 1</code> and more general one <code>Just n</code> that takes care of rest of the cases.</p>
<pre><code>isBig :: Maybe Int -&gt; String
isBig Nothing =
    &quot;Not at all&quot;

isBig (Just 1) =
    &quot;Just perfect&quot;

isBig (Just n) =
    if n &lt; 10
        then &quot;Just slightly&quot;
        else &quot;Definitely is&quot;</code></pre>
<p>Some example usage:</p>
<pre><code>&gt; isBig Nothing
&quot;Not at all&quot;
&gt; isBig $ Just 0
&quot;Just perfect&quot;
&gt; isBig $ Just 50
&quot;Definitely is&quot;</code></pre>
<p>Pattern matching isn’t limited to algebraic datatypes that we have been working with so far. We can do same things with records. Below is an function used to calculate total fee when cost and customer are known. Each customer can have their own discount percentage, but in addition we’re giving 10% discount to VIP customers:</p>
<pre><code>data Customer = Customer
    { customerName :: String
    , customerDiscountPct :: Double
    , vipCustomer :: Bool
    }

totalFee :: Double -&gt; Customer -&gt; Double
totalFee bill cust@(Customer { vipCustomer = True }) =
    bill * 0.9 * customerDiscountPct cust

totalFee bill cust =
    bill * customerDiscountPct cust</code></pre>
<p>There’s two cases of <code>totalFee</code> function. First one is for when passed in <code>Customer</code> has <code>vipCustomer</code> field <code>True</code>. Second one takes care of general case. In the first case we’re using <code>@</code> to bind <code>Customer</code> as a whole to <code>cust</code> name.</p>
<p>Lists can be matched too. The basic idea is exactly the same:</p>
<ul>
<li><code>(x:xs)</code> matches a list with at least one item, <code>x</code> is first item, <code>xs</code> is rest of the items (might be an empty list)</li>
<li><code>(x:y:_)</code> matches two first items in a list of at least two items, <code>x</code> is first, <code>y</code> is second, <code>_</code> is rest</li>
<li><code>[]</code> matches empty list</li>
<li><code>(x:[])</code> matches list of exactly one item</li>
</ul>
<p>Underscore <code>_</code> matches to everything without binding value to a name. This is useful when you don’t care about exact value, so you don’t want to give it a name. One could give it a name, but compiler will issue a warning if there are unused values in the code.</p>
<p>Next example is recursively counting amount if items in a list using pattern matching:</p>
<pre><code>count :: [a] -&gt; Int
count [] =
    0

count (x:xs) =
    1 + count xs</code></pre>
<p>Fibonacci series is series of number which starts with 0, 1 and then rest of the numbers are sum of two previous ones: 0, 1, 1, 2, 3, 5, 8…</p>
<p>To calculate number in series, we can write following code (this is extremely slow way of calculating them by the way):</p>
<pre><code>fibonacci :: Int -&gt; Int
fibonacci 0 =
    0

fibonacci 1 =
    1

fibonacci n =
    fibonacci (n - 1) + fibonacci (n - 2)</code></pre>
<p>Last trick in our sleeve for now is <code>case</code> expression. This allows us to do pattern matching inside of a function. Otherwise it works in the same way. Our fibonacci function could be defined as:</p>
<pre><code>fibonacci :: Int -&gt; Int
fibonacci n =
    case n of
        0 -&gt;
            0

        1 -&gt;
            1

        n -&gt;
            fibonacci (n - 1) + fibonacci (n - 2)</code></pre>
<p>Questions, comments and feedback are welcome. Best way to catch me nowadays is either email or in fediverse where I’m <code>Tuula@mastodon.social</code></p>
