Haskell/Next steps

This chapter introduces pattern matching and two new pieces of syntax:  expressions and   bindings.

if / then / else
Haskell syntax supports garden-variety conditional expressions of the form if... then... else.... For instance, consider a function that returns  if its argument is less than  ;   if its argument is  ; and   if its argument is greater than. The predefined  function does that job already; but for the sake of illustration, let's define a version of our own:

The parentheses around "-1" in the last example are required; if missing, Haskell will think that you are trying to subtract  from   (which would give a type error).

In an if/then/else construct, first the condition (in this case ) is evaluated. If it results, the whole construct evaluates to the then expression; otherwise (if the condition is  ), the construct evaluates to the else expression. All of that is pretty intuitive. If you have programmed in an imperative language before, however, it might seem surprising to know that Haskell always requires both a then and an else clause. The construct has to result in a value in both cases and, specifically, a value of the same type in both cases.

Function definitions using if / then / else like the one above can be rewritten using Guards.

Similarly, the absolute value function defined in Truth values can be rendered with an if/then/else:

Why use if/then/else versus guards? As you will see with later examples and in your own programming, either way of handling conditionals may be more readable or convenient depending on the circumstances. In many cases, both options work equally well.

Introducing pattern matching
Consider a program which tracks statistics from a racing competition in which racers receive points based on their classification in each race, the scoring rules being: We can write a simple function which takes a classification (represented by an integer number:  for first place, etc. ) and returns how many points were earned. One possible solution uses if/then/else:
 * 10 points for the winner;
 * 6 for second-placed;
 * 4 for third-placed;
 * 3 for fourth-placed;
 * 2 for fifth-placed;
 * 1 for sixth-placed;
 * no points for other racers.

Yuck! Admittedly, it wouldn't look this hideous had we used guards instead of if/then/else, but it still would be tedious to write (and read!) all those equality tests. We can do better, though:

Much better. However, even though defining  in this style (which we will arbitrarily call piece-wise definition from now on) shows to a reader of the code what the function does in a clear way, the syntax looks odd given what we have seen of Haskell so far. Why are there seven equations for ? What are those numbers doing in their left-hand sides? What about variable arguments?

This feature of Haskell is called pattern matching. When we call, the argument is matched against the numbers on the left side of each of the equations, which in turn are the patterns. The matching is done in the order we wrote the equations. First, the argument is matched against the  in the first equation. If the argument is indeed, we have a match and the first equation is used; so   evaluates to   as expected. Otherwise, the other equations are tried in order following the same procedure. The final one, though, is rather different: the  is a special pattern, often called a "wildcard", that might be read as "whatever": it matches with anything; and therefore if the argument doesn't match any of the previous patterns   will return zero.

As for the lack of  or any other variable standing for the argument, we simply don't need that to write the definitions. All possible return values are constants. Besides, variables are used to express relationships on the right side of the definition, so the  is unnecessary in our   function.

However, we could use a variable to make  even more concise. The points given to a racer decrease regularly from third place to sixth place, at a rate of one point per position. After noticing that, we can eliminate three of the seven equations as follows:

So, we can mix both styles of definitions. In fact, when we write  in the left side of an equation we are using pattern matching too! As a pattern, the  (or any other variable name) matches anything just like  ; the only difference being that it also gives us a name to use on the right side (which, in this case, is necessary to write  ).

Beyond integers, pattern matching works with values of various other types. One handy example is booleans. For instance, the  logical-or operator we met in Truth values could be defined as:

Or:

When matching two or more arguments at once, the equation will only be used if all of them match.

Now, let's discuss a few things that might go wrong when using pattern matching:


 * If we put a pattern which matches anything (such as the final patterns in each of the  example) before the more specific ones the latter will be ignored. GHC(i) will typically warn us that "Pattern match(es) are overlapped" in such cases.
 * If no patterns match, an error will be triggered. Generally, it is a good idea to ensure the patterns cover all cases, in the same way that the  guard is not mandatory but highly recommended.
 * Finally, while you can play around with various ways of (re)defining here is one version that will not work:


 * The program won't test whether the arguments are equal just because we happened to use the same name for both. As far as the matching goes, we could just as well have written  in the first case. And even worse: because we gave the same name to both arguments, GHC(i) will refuse the function due to "Conflicting definitions for `x'".

Tuple and list patterns
While the examples above show that pattern matching helps in writing more elegant code, that does not explain why it is so important. So, let's consider the problem of writing a definition for, the function which extracts the first element of a pair. At this point, that appears to be an impossible task, as the only way of accessing the first value of the pair is by using  itself... The following function, however, does the same thing as  (confirm it in GHCi):

It's magic! Instead of using a regular variable in the left side of the equation, we specified the argument with the pattern of the 2-tuple - that is,  - filled with a variable and the   pattern. Then the variable was automatically associated with the first component of the tuple, and we used it to write the right side of the equation. The definition of  is, of course, analogous.

Furthermore, the trick demonstrated above can be done with lists as well. Here are the actual definitions of  and  :

The only essential change in relation to the previous example was replacing  with the pattern of the cons operator. These functions also have an equation using the pattern of the empty list, ; however, since empty lists have no head or tail there is nothing to do other than use   to print a prettier error message.

In summary, the power of pattern matching comes from its use in accessing the parts of a complex value. Pattern matching on lists, in particular, will be extensively deployed in Recursion and the chapters that follow it. Later on, we will explore what is happening behind this seemingly magical feature.

let bindings
To conclude this chapter, a brief word about  bindings (an alternative to   clauses for making local declarations). For instance, take the problem of finding the roots of a polynomial of the form $$ax^2+bx+c$$ (in other words, the solution to a second degree equation — think back to your middle school math courses). Its solutions are given by:
 * $$x = \frac {-b \pm \sqrt{b^2-4ac}} {2a}$$.

We could write the following function to compute the two values of $$x$$:

Writing the  term in both cases is annoying, though; we can use a local binding instead, using either   or, as will be demonstrated below, a   declaration:

We put the  keyword before the declaration, and then use   to signal we are returning to the "main" body of the function. It is possible to put multiple declarations inside a single ... block — just make sure they are indented the same amount or there will be syntax errors: