# Programming in Haskell Solutions to Exercises

Graham Hutton University of Nottingham

Contents
Chapter 1 - Introduction Chapter 2 - First steps Chapter 3 - Types and classes Chapter 4 - Deﬁning functions Chapter 5 - List comprehensions Chapter 6 - Recursive functions Chapter 7 - Higher-order functions Chapter 8 - Functional parsers Chapter 9 - Interactive programs Chapter 10 - Declaring types and classes Chapter 11 - The countdown problem Chapter 12 - Lazy evaluation Chapter 13 - Reasoning about programs 1 3 4 5 7 9 13 15 19 21 23 24 26

Chapter 1 - Introduction
Exercise 1
= = = = = or = = = = = = double (double 2) { applying the outer double } (double 2) + (double 2) { applying the second double } (double 2) + (2 + 2) { applying the second + } (double 2) + 4 { applying double } (2 + 2) + 4 { applying the ﬁrst + } 4+4 { applying + } 8 double (double 2) { applying the inner double } double (2 + 2) { applying double } (2 + 2) + (2 + 2) { applying the ﬁrst + } 4 + (2 + 2) { applying the second + } 4+4 { applying + } 8

There are a number of other answers too.

Exercise 2
sum [x ] { applying sum } x + sum [ ] = { applying sum } x +0 = { applying + } x =

Exercise 3
(1) product [ ] = product (x : xs) = 1 x ∗ product xs

1

(2) product [2, 3, 4] { applying product } 2 ∗ (product [3, 4]) { applying product } 2 ∗ (3 ∗ product [4]) { applying product } 2 ∗ (3 ∗ (4 ∗ product [ ])) { applying product } 2 ∗ (3 ∗ (4 ∗ 1)) { applying ∗ } 24

= = = = =

Exercise 4
Replace the second equation by qsort (x : xs) = qsort larger + [x ] + qsort smaller + + That is, just swap the occurrences of smaller and larger .

Exercise 5
Duplicate elements are removed from the sorted list. For example: = = = = = qsort [2, 2, 3, 1, 1] { applying qsort } qsort [1, 1] + [2] + qsort [3] + + { applying qsort } (qsort [ ] + [1] + qsort [ ]) + [2] + (qsort [ ] + [3] + qsort [ ]) + + + + + + { applying qsort } ([ ] + [1] + [ ]) + [2] + ([ ] + [3] + [ ]) + + + + + + { applying + } + [1] + [2] + [3] + + { applying + } + [1, 2, 3]

2

5] Exercise 4 last xs or last xs = xs !! (length xs − 1) = head (reverse xs) Exercise 5 init xs or init xs = reverse (tail (reverse xs)) = take (length xs − 1) xs 3 . 2.First steps Exercise 1 (2 ↑ 3) ∗ 4 (2 ∗ 3) + (4 ∗ 5) 2 + (3 ∗ (4 ↑ 5)) Exercise 2 No solution required.Chapter 2 . Exercise 3 n = a ‘div ‘ length xs where a = 10 xs = [1. 3. 4.

and checking if the functions give the same result for each of these values. Char ) [(Bool .Types and classes Exercise 1 [Char ] (Char . a) a → b → (a. Exercise 4 In general.Chapter 3 . b) Num a ⇒ a → a Eq a ⇒ [a ] → Bool (a → a) → a → a Exercise 3 No solution required. 4 . it is feasible. However. b) → (b. such as values of type of type Bool . for small numbers of argument values. checking if two functions are equal requires enumerating all possible argument values. For functions with a very large (or inﬁnite) number of argument values. such as values of type Int or Integer. Char . Char )] ([Bool ]. this is not feasible. [Char ]) [[a ] → [a ]] Exercise 2 [a ] → a (a.

Deﬁning functions Exercise 1 halve xs or halve xs = (take n xs.Chapter 4 . drop n xs) where n = length xs ‘div ‘ 2 = splitAt (length xs ‘div ‘ 2) xs Exercise 2 (a) safetail xs (b) safetail xs | null xs | otherwise (c) safetail [ ] safetail xs or safetail [ ] = safetail ( : xs) = [] xs = = [] tail xs = = [] tail xs = if null xs then [ ] else tail xs Exercise 3 (1) False ∨ False False ∨ True True ∨ False True ∨ True (2) False ∨ False ∨ (3) False ∨ b True ∨ (4) b ∨ c | b == c | otherwise = = b True = = b True = False = True = = = = False True True True 5 .

Exercise 4 a∧b = if a then if b then True else False else False Exercise 5 a∧b = if a then b else False Exercise 6 mult = λx → (λy → (λz → x ∗ y ∗ z )) 6 .

List comprehensions Exercise 1 sum [x ↑ 2 | x ← [1 . n ]. n ]. 2. y) | y ← [4. ← [1 . sum (init (factors x )) == x ] Exercise 5 concat [[(x . 3]] Exercise 6 positions x xs = ﬁnd x (zip xs [0 . 100]] Exercise 2 replicate n x = [x | ← [1 . ’z’]] where xs = map toLower xs n = letters xs Char → Int ord c − ord ’a’ 7 low2int low2int c :: = . ↑ 2 + y ↑ 2 == z ↑ 2] Exercise 4 perfects n = [x | x ← [1 . y. 6]] | x ← [1. . . n ]. n ]. 5. . . . z ) | x y z x ← [1 . y) ← zip xs ys ] Exercise 8 shift shift n c | isLower c | isUpper c | otherwise freqs freqs xs :: = = = :: = Int → Char → Char int2low ((low2int c + n) ‘mod ‘ 26) int2upp ((upp2int c + n) ‘mod ‘ 26) c String → [Float ] [percent (count x xs ) n | x ← [’a’ . n ]] Exercise 3 pyths n = [(x .Chapter 5 . . . n ]) where n = length xs − 1 Exercise 7 scalarproduct xs ys = sum [x ∗ y | (x . . ← [1 .

int2low int2low n upp2int upp2int c int2upp int2upp n letters letters xs :: = :: = :: = :: = Int → Char chr (ord ’a’ + n) Char → Int ord c − ord ’A’ Int → Char chr (ord ’A’ + n) String → Int length [x | x ← xs. isAlpha x ] 8 .

4.Chapter 6 . 5] } } } } 9 . 5] { applying drop drop 2 [2. 4. 2.Recursive functions Exercise 1 (1) m ↑0 = 1 m ↑ (n + 1) = m ∗ m ↑ n (2) = = = = = 2↑3 { applying ↑ } 2 ∗ (2 ↑ 2) { applying ↑ } 2 ∗ (2 ∗ (2 ↑ 1)) { applying ↑ } 2 ∗ (2 ∗ (2 ∗ (2 ↑ 0))) { applying ↑ } 2 ∗ (2 ∗ (2 ∗ 1)) { applying ∗ } 8 Exercise 2 (1) = = = = = length [1. 3] { applying length } 1 + length [2. 4. 3. 5] = { applying drop drop 1 [3. 5] = { applying drop [4. 5] = { applying drop drop 0 [4. 2. 3] { applying length } 1 + (1 + length [3]) { applying length } 1 + (1 + (1 + length [ ])) { applying length } 1 + (1 + (1 + 0)) { applying + } 3 (2) = drop 3 [1. 3.

2] = Exercise 3 and [ ] and (b : bs) concat [ ] concat (xs : xss) replicate 0 replicate (n + 1) x (x : ) !! 0 ( : xs) !! (n + 1) elem x [ ] elem x (y : ys) | x == y | otherwise = = = = = = = = = = = True b ∧ and bs [] xs + concat xss + [] x : replicate n x x xs !! n False True elem x ys Exercise 4 merge [ ] ys merge xs [ ] merge (x : xs) (y : ys) = ys = xs = if x ≤ y then x : merge xs (y : ys) else y : merge (x : xs) ys Exercise 5 halve xs = splitAt (length xs ‘div ‘ 2) xs msort [ ] = [] msort [x ] = [x ] msort xs = merge (msort ys) (msort zs) where (ys.(3) init [1. zs) = halve xs 10 . 3] = { applying init } 1 : 2 : init [3] = { applying init } 1 :2 : [] = { list notation } [1. 3] { applying init } 1 : init [2. 2.

2 Step 1: deﬁne the type take :: Int → [a ] → [a ] 0 [] 0 (x : xs) (n + 1) [ ] (n + 1) (x : xs) = = = = Step 2: enumerate the cases take take take take Step 3: deﬁne the simple cases take take take take 0 [] 0 (x : xs) (n + 1) [ ] (n + 1) (x : xs) = = = = [] [] [] Step 4: deﬁne the other cases take take take take 0 [] 0 (x : xs) (n + 1) [ ] (n + 1) (x : xs) = = = = [] [] [] x : take n xs Int → [a ] → [a ] [] [] x : take n xs Step 5: generalise and simplify take :: = take 0 take (n + 1) [ ] = take (n + 1) (x : xs) = 11 .Exercise 6.1 Step 1: deﬁne the type sum :: [Int ] → Int = = Step 2: enumerate the cases sum [ ] sum (x : xs) Step 3: deﬁne the simple cases sum [ ] sum (x : xs) = 0 = Step 4: deﬁne the other cases sum [ ] sum (x : xs) = 0 = x + sum xs Step 5: generalise and simplify sum sum :: = Num a ⇒ [a ] → a foldr (+) 0 Exercise 6.

Exercise 6.3 Step 1: deﬁne the type last :: [a ] → [a ] Step 2: enumerate the cases last (x : xs) = Step 3: deﬁne the simple cases last (x : xs) | null xs | otherwise Step 4: deﬁne the other cases last (x : xs) | null xs | otherwise Step 5: generalise and simplify last :: last [x ] = last ( : xs) = [a ] → [a ] x last xs = x = last xs = x = 12 .

Higher-order functions Exercise 1 map f (ﬁlter p xs) Exercise 2 all p any p = = and ◦ map p or ◦ map p takeWhile [ ] = [ ] takeWhile p (x : xs) |px = x : takeWhile p xs | otherwise = [ ] dropWhile [ ] dropWhile p (x |px | otherwise = [] : xs) = dropWhile p xs = x : xs Exercise 3 map f ﬁlter p = = foldr (λx xs → f x : xs) [ ] foldr (λx xs → if p x then x : xs else xs) [ ] Exercise 4 dec2nat = foldl (λx y → 10 ∗ x + y) 0 Exercise 5 The functions being composed do not all have the same types. y) → f x y 13 . For example: sum map (↑2) ﬁlter even :: [Int ] → Int :: [Int ] → [Int ] :: [Int ] → [Int ] Exercise 6 curry curry f uncurry uncurry f :: = :: = ((a.Chapter 7 . b) → c) → (a → b → c) λx y → f (x . b) → c) λ(x . y) (a → b → c) → ((a.

14 .Exercise 7 chop8 map f iterate f = = = unfold null (take 8) (drop 8) unfold null (f ◦ head ) tail unfold (const False) id f Exercise 8 encode encode decode decode addparity addparity bs :: = :: = :: = String → [Bit ] concat ◦ map (addparity ◦ make8 ◦ int2bin ◦ ord ) [Bit ] → String map (chr ◦ bin2int ◦ checkparity ) ◦ chop9 [Bit ] → [Bit ] (parity bs) : bs [Bit ] → Bit 1 0 [Bit ] → [[Bit ]] [] take 9 bits : chop9 (drop 9 bits) [Bit ] → [Bit ] bs error "parity mismatch" parity :: parity bs | odd (sum bs) = | otherwise = chop9 chop9 [ ] chop9 bits checkparity checkparity (b : bs) | b == parity bs | otherwise :: = = :: = = Exercise 9 No solution required.

Functional parsers Exercise 1 int = do char ’-’ n ← nat return (−n) + +nat + Exercise 2 comment = do string "--" many (sat (= ’\n’)) return () Exercise 3 (1) expr KK KK uuu  zu % expr exprI + II u I\$ uu  zu  expr expr + term    term term factor  factor  nat  2 (2) expr I ss  III yss \$ exprI expr + uu  III zuu  \$ expr expr + term  factor  nat  2  term  factor  nat  3  term  factor  nat  4  factor  nat  3  nat  4 15 .Chapter 8 .

Exercise 4 (1) expr I III uuu  \$ zu expr + term   factor term   factor nat   nat 2  3 (2) expr  termI t II I\$ yttt  ∗ factor termG GGG ww G# {ww   ∗ factor nat term    factor nat 2   nat 3  4 (3) expr K ss  KKK yss % expr + term   factorI term III uuu  II  \$ zuuu expr ) factor ( JJJ tt JJ   zttt \$ expr + term nat  factor  nat  2  term  factor  nat  3  4 16 .

For example.Exercise 5 Without left-factorising the grammar. the resulting parser would backtrack excessively and have exponential time complexity in the size of the expression. a number would be parsed four times before being recognised as an expression. Exercise 6 expr = do t ← term do symbol "+" e ← expr return (t + e) + + do symbol "-" + e ← expr return (t − e) + + return t + = do f ← factor do symbol "*" t ← term return (f ∗ t ) + + do symbol "/" + t ← term return (f ‘div ‘ t ) + + return f + term Exercise 7 (1) factor atom (2) factor factor :: Parser Int = do a ← atom do symbol "^" f ← factor return (a ↑ f ) + + return a + :: = Parser Int do symbol "(" e ← expr symbol ")" return e + + natural + ::= ::= atom (↑ factor | epsilon) (expr ) | nat atom atom 17 .

Exercise 8 (a) expr nat (b) expr = do e ← expr symbol "-" n ← natural return (e − n) + + natural + ::= ::= expr − nat | nat 0 | 1 | 2 | ··· (c) The parser loops forever without producing a result. because the ﬁrst operation it performs is to call itself recursively. (d) expr = do n ← natural ns ← many (do symbol "-" natural) return (foldl (−) n ns) 18 .

Exercise 5 No solution available. 2. Exercise 3 No solution available. 4. Exercise 6 type Board initial initial ﬁnished ﬁnished b valid valid b row num move move b row num newline newline = :: = :: = :: = :: = :: = [Int ] Board [5. .Chapter 9 . Exercise 4 No solution available. n) ← zip [1 . 1] Board → Bool all (== 0) b Board → Int → Int → Bool b !! (row − 1) ≥ num Board → Int → Int → Board [if r == row then n − num else n | (r .Interactive programs Exercise 1 readLine get xs = get "" = do x ← getChar case x of ’\n’ → return xs ’\DEL’ → if null xs then get xs else do putStr "\ESC[1D \ESC[1D" get (init xs) → get (xs + [x ]) + Exercise 2 No solution available. 5] b ] IO () putChar ’\n’ 19 . 3.

d .putBoard putBoard [a. e ] :: = Board → IO () do putRow 1 a putRow 2 b putRow 3 c putRow 4 d putRow 5 e Int → Int → IO () do putStr (show row ) putStr ": " putStrLn (stars num) Int → String concat (replicate n "* ") String → IO Int do putStr prom x ← getChar newline if isDigit x then return (ord x − ord ’0’) else do putStrLn "ERROR: Invalid digit" getDigit prom IO () play initial 1 Board → Int → IO () do newline putBoard board if ﬁnished board then do newline putStr "Player " putStr (show (next player )) putStrLn " wins!!" else do newline putStr "Player " putStrLn (show player ) r ← getDigit "Enter a row number: " n ← getDigit "Stars to remove : " if valid board r n then play (move board r n) (next player ) else do newline putStrLn "ERROR: Invalid move" play board player Int → Int 2 1 putRow putRow row num :: = stars stars n getDigit getDigit prom :: = :: = nim nim play play board player :: = :: = next next 1 next 2 :: = = 20 . b. c.

21 .Declaring types and classes Exercise 1 mult m Zero mult m (Succ n) = Zero = add m (mult m n) Exercise 2 occurs m (Leaf n) = occurs m (Node l n r ) = m == n case compare m n of LT → occurs m l EQ → True GT → occurs m r This version is more eﬃcient because it only requires one comparison for each node. zs) = halve xs balance [x ] = balance xs = Exercise 5 data Prop eval s (Or p q) eval s (Equiv p q) vars (Or p q) vars (Equiv p q) = · · · | Or Prop Prop | Equiv Prop Prop = eval s p ∨ eval s q = eval s p == eval s q = vars p + vars q + = vars p + vars q + Exercise 6 No solution available. whereas the previous version may require two comparisons. Exercise 3 leaves (Leaf ) leaves (Node l r ) = = 1 leaves l + leaves r True abs (leaves l − leaves r ) ≤ 1 ∧ balanced l ∧ balanced r balanced (Leaf ) = balanced (Node l r ) = Exercise 4 halve xs = splitAt (length xs ‘div ‘ 2) xs Leaf x Node (balance ys) (balance zs) where (ys.Chapter 10 .

Exercise 7 data Expr type Cont data Op eval eval (Val n) ops eval (Add x y) ops eval (Mult x y) ops exec exec exec exec exec exec [] n (EVALA y : ops) n (ADD n : ops) m (EVALM y : ops) n (MUL n : ops) m = Val Int | Add Expr Expr | Mult Expr Expr = [Op ] = EVALA Expr | ADD Int | EVALM Expr | MUL Int :: = = = :: = = = = = Expr → Cont → Int exec ops n eval x (EVALA y : ops) eval x (EVALM y : ops) Cont → Int → Int n eval y (ADD n : ops) exec ops (n + m) eval y (MUL n : ops) exec ops (n ∗ m) value value e :: Expr → Int = eval e [ ] Exercise 8 instance Monad Maybe where return :: a → Maybe a return x = Just x (>=) > Nothing >= > (Just x ) >= f > :: Maybe a → (a → Maybe b) → Maybe b = Nothing = f x instance Monad [ ] where return :: a → [a ] return x = [x ] (>=) > xs >= f > :: [a ] → (a → [b ]) → [b ] = concat (map f xs) 22 .

50]. because recursive calls to exprs would no longer be guaranteed to reduce the length of the list. 23 . 25. e ← exprs ns. 7. 10. 7. 25. zs ← perms ys ] Exercise 2 removeone x [ ] = [] removeone x (y : ys) | x == y = ys | otherwise = y : removeone x ys isChoice [ ] isChoice (x : xs) [ ] isChoice (x : xs) ys = True = False = elem x ys ∧ isChoice xs (removeone x ys) Exercise 3 It would lead to non-termination. 50].The countdown problem Exercise 1 choices xs = [zs | ys ← subs xs. 3. e ← exprs ns . 3.Chapter 11 . Exercise 4 > length [e | ns ← choices [1. 10. 50]. eval e = [ ]] 10839369 = True = y = 0 ∧ x ‘mod ‘ y == 0 Exercise 6 No solution available. e ← exprs ns ] 33665406 > length [e | ns ← choices [1. eval e = [ ]] 4672540 Exercise 5 Modifying the deﬁnition of valid by valid Sub x y valid Div x y gives > length [e | ns ← choices [1. 25. 3. 7. 10.

Exercise 2 Outermost: fst (1 + 2. (2) 1 + 2 and 2 + 3 are redexes. 2 + 3) are redexes.Chapter 12 . and is both innermost and outermost. 2 + 3 and fst (1 + 2.Lazy evaluation Exercise 1 (1) 2 ∗ 3 is the only redex. with 1 + 2 being innermost. 5) = { applying fst } 3 Outermost evaluation is preferable because it avoids evaluation of the second argument. and hence takes one less reduction step. 2 + 3) { applying fst } 1+2 = { applying + } 3 = Innermost: = fst (1 + 2. Exercise 3 mult 3 4 { applying mult } (λx → (λy → x ∗ y)) 3 4 = { applying λx → (λy → x ∗ y) } (λy → 3 ∗ y) 4 = { applying λy → 3 ∗ y } 3∗4 = { applying ∗ } 12 = 24 . (3) 1 + 2. 2 + 3) = { applying + } fst (3. with the ﬁrst being innermost and the second being outermost. (4) 2 ∗ 3 and (λx → 1 + x ) (2 ∗ 3) are redexes. with the ﬁrst of these being innermost and the last being outermost. 2 + 3) { applying the ﬁrst + } fst (3.

Exercise 4 ﬁbs = 0 : 1 : [x + y | (x . y) ← zip ﬁbs (tail ﬁbs)] Exercise 5 (1) ﬁb n (2) head (dropWhile (≤ 1000) ﬁbs) = ﬁbs !! n Exercise 6 repeatTree repeatTree x takeTree takeTree 0 takeTree (n + 1) Leaf takeTree (n + 1) (Node l x r ) replicateTree replicateTree n :: a → Tree a = Node t x t where t = repeatTree x :: = = = Int → Tree a → Tree a Leaf Leaf Node (takeTree n l ) x (takeTree n r ) :: Int → a → Tree a = takeTree n ◦ repeatTree 25 .