The following are not part of the current specification. I've included in case you want to get more of a taste of Haskell…
# Pattern Matching
Haskell allows pattern matching. The following function counts one, two or many objects
```haskell
simpleCount 1 = "One"
simpleCount 2 = "Two"
simpleCount x = "Many"
```
You can use pattern matching to set base cases in recursive functions. Here's an example of a factorial function from later on. Note how factorial 0 is defined as being 1 using pattern matching.
```haskell
factorial 0 = 1
factorial n = n \* factorial (n-1)
```
# Ifs and Guards
Haskell allows you to use ifs and guards in your functions. A guard is a more readable version of an if. People can learn to drive from the age of 17 in the UK. The following function uses to an if statement to check if someone is old enough to drive
```haskell
canDrive x = if x <18 then "Too young to drive" else "Old enough to drive"
```
Here it is using guards:
```haskell
canDrive' x
| x<18 = "Too young to drive" |
| otherwise = "Old enough to drive" |
```
The following function uses guards to work out the cost in pence of sending a large letter in the UK
```haskell
letterCost weight
| weight <= 100 = 96
| weight <= 250 = 127
| weight <= 500 = 171
| otherwise = 2.46
```
Looking at the above letterCost function you might reasonably deduce that you could send an elephant via the UK postal service for £2.46. Unfortunately, the prices given are only for large letters which can weigh no more than 1kg.
# Show
What if you want to mix text and numbers when using ifs and guards? For example, in the game of fizz, you say "Fizz" if a number is divisible by 3, otherwise you just say the number. Writing a funciton to implement this can cause a problem in Haskell, as the function will have to return text or a number. One way round this is to convert the number to text using show, as follows.
```haskell
fizz n
| n `mod` 3 == 0 = "Fizz"
| otherwise = show n
```
# Pattern Matching, Ifs and Guards Exercise
Write the following functions:
1. A function that returns "Zero" if 0 is passed, then "Odd Number" or "Even Number" if an odd or even number is passed.
2. A grade function that calculates students grade as follows: A >50, B >40, C>30 otherwise fail
3. A fizz function, the returns "Fizz" if a number is divisible by three, otherwise it just returns the number
4. A buzz function, the returns "Buzz" if a number is divisible by five, otherwise it just returns the number
5. A FizzBuzz Function that returns "FizzBuzz" if a number is divisible by 3 and 5, Fizz if it's divisible by three and Buzz if it's divisible by five. Otherwise, just return the number.
# Functions and List Comprehensions
Let's define a function that uses a list comprehension to find the factors of n
```haskell
factors n = [x | x <- [1..n], n `mod` x == 0]
```
Now, remembering that a number n is prime if and only if it has two factors, \[1,n\], let's define function to determine if a number is prime
```haskell
prime n = factors n == [1,n]
*Main> factors 15
[1,3,5,15]
*Main> factors 7
[1,7]
*Main> prime 7
True
*Main> prime 2
True
```
[[Recursive Functions in Haskell]]
[[Putting it All Together]]