-- `fact0 n` returns the factorial of n fact0 :: Integer -> Integer fact0 n = if n == 0 then 1 else n * fact0(n-1) {- `fact1 n` also returns the factorial of n. However, it's written using pattern matching. -} fact1 :: Integer -> Integer fact1 0 = 1 fact1 n = n * fact1 (n-1) -- `fact2` is also the factorial function; it uses builtin functions fact2 :: Integer -> Integer fact2 n = product [1..n] -- `fib n` returns the nth Fibonacci number fib :: Int -> Integer fib 0 = 0 fib 1 = 1 fib n = fib (n-1) + fib (n-2) -- `sum list` returns the sum of all numbers in list sum' :: [Integer] -> Integer sum' [] = 0 sum' (x:xs) = x + sum' xs -- `isMember x list` returns True iff x is a member of list isMember :: Eq a => a -> [a] -> Bool isMember x list = if null list then False else if x == (head list) then True else isMember x (tail list) {- `isMember' x list` still returns True iff x is a member of list. However, it's written using pattern matching. -} isMember' :: Eq a => a -> [a] -> Bool isMember' _ [] = False isMember' y (x:xs) = if y == x then True else isMember' y xs {- `take'` works exactly the same as the prelude `take` -} take' :: Int -> [a] -> [a] take' _ [] = [] take' 0 _ = [] take' n (x:xs) = x:(take' (n-1) xs)