## Revised sections of Next Steps

### case

Haskell, like many other languages, also supports case constructions. These are used when there are multiple values that you want to check against (case expressions are actually quite a bit more powerful than this – see the Pattern matching chapter for all of the details).

Suppose we wanted to define a function that had a value of ${\displaystyle 1}$ if its argument were ${\displaystyle 0}$; a value of ${\displaystyle 5}$ if its argument were ${\displaystyle 1}$; a value of ${\displaystyle 2}$ if its argument were ${\displaystyle 2}$; and a value of ${\displaystyle -1}$ in all other instances. Writing this function using if statements would be long and very unreadable; so we write it using a case statement as follows (we call this function f):

Example:

f x =
case x of
0 -> 1
1 -> 5
2 -> 2
_ -> -1


In this program, we're defining f to take an argument x and then inspecting the value of x. If it matches ${\displaystyle 0}$, the value of f is ${\displaystyle 1}$. If it matches ${\displaystyle 1}$, the value of f is ${\displaystyle 5}$. If it matches ${\displaystyle 2}$, then the value of f is ${\displaystyle 2}$; and if it hasn't matched anything by that point, the value of f is ${\displaystyle -1}$ (the underscore can be thought of as a "wildcard" – it will match anything).

The indentation here is important. Haskell uses a system called "layout" to structure its code (the programming language Python uses a similar system). The layout system allows you to write code without the explicit semicolons and braces that other languages like C and Java require.

### Indentation

The general rule for layout is that an opening brace (the { character) is inserted after the keywords where, let, do and of, and the column position at which the next command appears is remembered. From then on, a semicolon is inserted before every new line that is indented the same amount. If a following line is indented less, a closing brace (}) is inserted. This may sound complicated, but if you follow the general rule of indenting after each of those keywords, you'll never have to remember it (see the Indentation chapter for a more complete discussion of layout).

Some people prefer not to use layout and write the braces and semicolons explicitly. This is perfectly acceptable. In this style, the above function might look like:

f x = case x of
{ 0 -> 1 ; 1 -> 5 ; 2 -> 2 ; _ -> -1 }


Of course, if you write the braces and semicolons explicitly, you're free to structure the code as you wish. The following is also equally valid:

f x =
case x of { 0 -> 1 ;
1 -> 5 ; 2 -> 2
; _ -> -1 }


However, structuring your code like this only serves to make it unreadable (in this case).

### Defining one function for different parameters

Functions can also be defined piece-wise, meaning that you can write one version of your function for certain parameters and then another version for other parameters. For instance, the above function f could also be written as:

Example:

f 0 = 1
f 1 = 5
f 2 = 2
f _ = -1


Just like in the case situation above, order is important. If we had put the last line first, it would have matched every argument, and f would return -1, regardless of its argument (most compilers will warn you about this, though, saying something about overlapping patterns). If we had not included this last line, f would produce an error if anything other than 0, 1 or 2 were applied to it (most compilers will warn you about this, too, saying something about incomplete patterns). This style of piece-wise definition is very popular and will be used quite frequently throughout this tutorial. These two definitions of f are actually equivalent – this piece-wise version is translated into the case expression.

### Function composition

More complicated functions can be built from simpler functions using function composition. Function composition is simply taking the result of the application of one function and using that as an argument for another. We've already seen this back in the Getting set up chapter, when we wrote 5*4+3. In this, we were evaluating ${\displaystyle 5\cdot 4}$ and then applying ${\displaystyle +3}$ to the result. We can do the same thing with our square and f functions:

Example:

square x = x^2

*Main> square (f 1)
25
*Main> square (f 2)
4
*Main> f (square 1)
5
*Main> f (square 2)
-1

The result of each of these function applications is fairly straightforward. The parentheses around the inner function are necessary; otherwise, in the first line, the interpreter would think that you were trying to get the value of square f, which has no meaning. Function application like this is fairly standard in most programming languages.

There is another, more mathematical way to express function composition: the (.) enclosed period function. This (.) function is modeled after the (${\displaystyle \circ }$) operator in mathematics.

Note

In mathematics we write ${\displaystyle f\circ g}$ to mean "f following g." In Haskell , we write f . g also to mean "f following g."

The meaning of ${\displaystyle f\circ g}$ is simply that ${\displaystyle (f\circ g)(x)=f(g(x))}$. That is, applying the function ${\displaystyle f\circ g}$ to the value ${\displaystyle x}$ is the same as applying ${\displaystyle g}$ to ${\displaystyle x}$, taking the result, and then applying ${\displaystyle f}$ to that.

The (.) function (called the function composition function), takes two functions and makes them into one. For instance, if we write (square . f), this means that it creates a new function that takes an argument, applies f to that argument and then applies square to the result. Conversely, (f . square) means that a new function is created that takes an argument, applies square to that argument and then applies f to the result. We can see this by testing it as before:

Example:

*Main> (square . f) 1
25
*Main> (square . f) 2
4
*Main> (f . square) 1
5
*Main> (f . square) 2
-1

Here, we must enclose the function composition in parentheses; otherwise, the Haskell compiler will think we're trying to compose square with the value f 1 in the first line, which makes no sense since f 1 isn't even a function.

## Remotions from Simple I/O

### Controlling actions

(...)

We can write the same doGuessing function using a case statement. To do this, we first introduce the Prelude function compare, which takes two values of the same type (in the Ord class) and returns a value of type Ordering, namely one of GT, LT, EQ, depending on whether the first is greater than, less than or equal to the second.

doGuessing num = do
guess <- getLine
case compare (read guess) num of
LT -> do putStrLn "Too low!"
doGuessing num
GT -> do putStrLn "Too high!"
doGuessing num
EQ -> putStrLn "You Win!"


Here, again, the dos after the ->s are necessary on the first two options, because we are sequencing actions.

If you're used to programming in an imperative language like C or Java, you might think that return will exit you from the current function. This is not so in Haskell. In Haskell, return simply takes a normal value (for instance, one of type Int) and makes it into an action that when evaluated, gives the given value (for the same example, the action would be of type IO Int). In particular, in an imperative language, you might write this function as:

void doGuessing(int num) {
if (guess == num) {
print "You win!";
return ();
}

// we won't get here if guess == num
if (guess < num) {
print "Too low!";
doGuessing(num);
} else {
print "Too high!";
doGuessing(num);
}
}


Here, because we have the return () in the first if match, we expect the code to exit there (and in most imperative languages, it does). However, the equivalent code in Haskell, which might look something like:

doGuessing num = do
guess <- getLine
case compare (read guess) num of
EQ -> do putStrLn "You win!"
return ()

-- we don't expect to get here if guess == num
then do print "Too low!";
doGuessing num
else do print "Too high!";
doGuessing num


First of all, if you guess correctly, it will first print "You win!," but it won't exit, and it will check whether guess is less than num. Of course it is not, so the else branch is taken, and it will print "Too high!" and then ask you to guess again.

On the other hand, if you guess incorrectly, it will try to evaluate the case statement and get either LT or GT as the result of the compare. In either case, it won't have a pattern that matches, and the program will fail immediately with an exception.

Exercises

What does the following program print out?

main =
do x <- getX
putStrLn x

getX =
do return "hello"
return "aren't"
return "these"
return "returns"
return "rather"
return "pointless?"

Why?
Exercises

Write a program that asks the user for his or her name. If the name is one of Simon, John or Phil, tell the user that you think Haskell is a great programming language. If the name is Koen, tell them that you think debugging Haskell is fun (Koen Classen is one of the people who works on Haskell debugging); otherwise, tell the user that you don't know who he or she is.

Write two different versions of this program, one using if statements, the other using a case statement.

## Pattern matching (Where you can use pattern matching)

### Case expressions

Another typical usage of pattern binding is on the left hand side of case branches:

describeList :: (Show a) => [a] -> String
describeList list =
case list of
[]     -> "The list was empty"
(x:xs) -> "The list wasn't empty: the first element was " ++ show x ++ ", and " ++
"there were " ++ show (length xs) ++ " more elements in the list."


### Other places

There are a few other situations in which patterns can be used that we'll meet later on. Here's a list in case you're very eager already:

• where clauses, which are an alternative to let bindings;
• lambdas, which are anonymous function definitions;
• In p <- x assignments in do-blocks, p can be a pattern.
• Similarly, with let bindings in do-blocks, you can pattern match analogously to 'real' let bindings.