{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

solutions

# solutions - Programming in Haskell Solutions to Exercises...

This preview shows pages 1–7. Sign up to view the full content.

Programming in Haskell Solutions to Exercises Graham Hutton University of Nottingham Contents Chapter 1 - Introduction 1 Chapter 2 - First steps 3 Chapter 3 - Types and classes 4 Chapter 4 - Defining functions 5 Chapter 5 - List comprehensions 7 Chapter 6 - Recursive functions 9 Chapter 7 - Higher-order functions 13 Chapter 8 - Functional parsers 15 Chapter 9 - Interactive programs 19 Chapter 10 - Declaring types and classes 21 Chapter 11 - The countdown problem 23 Chapter 12 - Lazy evaluation 24 Chapter 13 - Reasoning about programs 26

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Chapter 1 - Introduction Exercise 1 double ( double 2) = { applying the inner double } double (2 + 2) = { applying double } (2 + 2) + (2 + 2) = { applying the first + } 4 + (2 + 2) = { applying the second + } 4 + 4 = { applying + } 8 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 first + } 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 [ ] = 1 product ( x : xs ) = 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

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Chapter 2 - First steps Exercise 1 (2 3) 4 (2 3) + (4 5) 2 + (3 (4 5)) Exercise 2 No solution required. Exercise 3 n = a div length xs where a = 10 xs = [1 , 2 , 3 , 4 , 5] Exercise 4 last xs = head ( reverse xs ) or last xs = xs !! ( length xs 1) Exercise 5 init xs = take ( length xs 1) xs or init xs = reverse ( tail ( reverse xs )) 3
Chapter 3 - Types and classes Exercise 1 [ Char ] ( Char , Char , Char ) [( Bool , Char )] ([ Bool ] , [ Char ]) [[ a ] [ a ]] Exercise 2 [ a ] a ( a , b ) ( b , a ) a b ( a , b ) Num a a a Eq a [ a ] Bool ( a a ) a a Exercise 3 No solution required. Exercise 4 In general, checking if two functions are equal requires enumerating all possible ar- gument values, and checking if the functions give the same result for each of these values. For functions with a very large (or infinite) number of argument values, such as values of type Int or Integer , this is not feasible. However, for small numbers of argument values, such as values of type of type Bool , it is feasible. 4

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Chapter 4 - Defining functions Exercise 1 halve xs = splitAt ( length xs div ‘ 2) xs or halve xs = ( take n xs , drop n xs ) where n = length xs div ‘ 2 Exercise 2 (a) safetail xs = if null xs then [ ] else tail xs (b) safetail xs | null xs = [ ] | otherwise = tail xs (c) safetail [ ] = [ ] safetail xs = tail xs or safetail [ ] = [ ] safetail ( : xs ) = xs Exercise 3 (1) False False = False False True = True True False = True True True = True (2) False False = False = True (3) False b = b True = True (4) b c | b == c = b |
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}