# Number Problems 6! (6 Factorial) = 6x5x4x3x2x1.  Write a recursive function fac that returns the factorial of a number.  Example: fac 4 returns 24 ```haskell fac 0 = 1 fac 1 = 1 fac x = x * fac (x-1) ``` The first 6 triangle numbers are 1, 3, 6, 10, 15. The nth triangle number is 1 + 2 + 3 + ... + n. Write a recursive method to find the nth triangle number ```haskell triangle 1 = 1 triangle x = x + triangle (x - 1) ``` Write a recursive function pow m n that returns m to the nth power.  Example:  pow 2 3 returns 8. ```haskell pow _ 0 = 1 pow m 1 = m pow m n = m * pow m (n-1) ``` The Harmonic Series begins 1 + 1/2 + 1/3 + 1/4 + 1/5 + ... + 1/n. Write a recursive method that finds the sum up to the nth term. ```haskell harm 1 = 1 harm n = (1/n) + harm (n-1) ``` From Wikipedia:  The Collatz conjecture is a conjecture in mathematics that concerns a sequence defined as follows: start with any positive integer n. Then each term is obtained from the previous term as follows: - If the previous term is even, the next term is one half the previous term. - Otherwise, the next term is 3 times the previous term plus 1. - The conjecture is that no matter what value of n, the sequence will always reach 1.   - Write a recursive function that applies the sequence to a number n.  Note that if the conjecture is true, your function will always return 1, it may just take some time to do this! - Hint: use ``n `div` 2`` rather than n/2 to stop Haskell being picky about types. ```haskell collatz 1 = 1 collatz n | even n = collatz (n `div` 2) | otherwise = collatz (3*n +1) ``` ## Extension Modify the above question so that it counts the number of steps before reaching 1 # List Problems Find the minimum value in a list. Example: minimum' \[1,3,2\] returns 1 ```haskell minimum' [x] = x minimum' (x:xs) = min x (minimum' xs) ``` Work out the product of a list (multiply all the numbers together) Example: product' \[2,3,4\] returns 24 ```haskell prodlist [x] = x prodlist (x:xs) = x * (prodlist xs) ``` Count the number of zeroes in a list. Example: noZeroes \[1,0,3,0,4\] returns 2 ```haskell noZeroes [] = 0 noZeroes (x:xs) | x == 0 = 1 + (noZeroes xs) | otherwise = (noZeroes xs) ``` Count the number of times an element appears in a list. Example numElem 4 \[1,4,2,4,5\] returns 2 ```haskell numElem _ [] = 0 numElem e (x:xs) | e == x = 1 + (numElem e xs) | otherwise = numElem e xs ``` Find last element in a list ```haskell lastElem [x] = x lastElem (x:xs) = lastElem xs ``` Take the first n elements from a list.  Example: take' 3 \[a,b,c,d,e\] returns \[a,b,c\]   ```haskell take' n _ | n<=0 = [] take' _ [] = [] take1 n (x:xs) = [x] ++ (take (n-1) xs) ``` Duplicate a list.  Example: duplicate' \[a,b,c\] returns \[a,a,b,b,c,c\] ```haskell duplicate' [] = [] duplicate' (x:xs) = [x, x] ++ (duplicate' xs) ``` ## Extension Remove repeated elements from a list.  Example: reduce' \[a,a,b,c,d,d,e\] returns \[a,b,c,d,e\] ```haskell reduce' [] = [] reduce' (x:xs) | null xs = [x] | x == head xs = reduce' xs | otherwise = x:reduce' xs ```