• Home
  • Textbooks
  • Programming in Haskell
  • Higher-order functions

Programming in Haskell

Graham Hutton

Chapter 7

Higher-order functions - all with Video Answers

Educators


Chapter Questions

Problem 1

Show how the list comprehension $[f \mathrm{x} \mid \mathrm{x}<-\mathrm{xs}, \mathrm{p}$ $x$ ] can be re-expressed using the higher-order functions map and filter.

Check back soon!

Problem 2

Without looking at the definitions from the standard prelude, define the following higher-order library functions on lists.
a. Decide if all elements of a list satisfy a predicate:
all :: $(a \rightarrow$ BOOl $) \rightarrow[\mathrm{BOOl}]->$ BOOl
b. Decide if any element of a list satisfies a predicate:
any $::(a \rightarrow$ BOOl $) \rightarrow[$ BOOl] $\rightarrow$ BOOL
c. Select elements from a list while they satisfy a predicate:
takeWhile :: (a $\rightarrow$ Bool $) \rightarrow[a] \rightarrow[a]$
d. Remove elements from a list while they satisfy a predicate:
dropWhile :: (a $\rightarrow$ Bool $) \rightarrow[a]->$ [a]
Note: in the prelude the first two of these functions are generic functions rather than being specific to the type of lists.

Check back soon!

Problem 3

Redefine the functions map $f$ and filter $p$ using foldr.

Check back soon!

Problem 4

Using foldl, define a function dec2int :: [Int] $\rightarrow$ Int that converts a decimal number into an integer. For example:
dec2int $[2,3,4,5]$
2345

Check back soon!

Problem 5

Without looking at the definitions from the standard prelude, define the higher-order library function curry that converts a function on pairs into a curried function, and, conversely, the function uncurry that converts a curried function with two arguments into a function on pairs. Hint: first write down the types of the two functions.

Check back soon!

Problem 6

A higher-order function unfold that encapsulates a simple pattern of recursion for producing a list can be defined as follows: That is, the function unfold $\mathrm{p} \mathrm{h}$ t produces the empty list if the predicate $\mathrm{p}$ is true of the argument value, and otherwise produces a non-empty list by applying the function $\mathrm{h}$ to this value to give the head, and the function $t$ to generate another argument that is recursively processed in the same way to produce the tail of the list. For example, the function int2bin can be rewritten more compactly using unfold as follows:
$$
\text { int2bin }=\text { unfold }(==0)(\text { mod' 2) ('div' 2) }
$$
Redefine the functions chop8, map $\mathrm{f}$ and iterate $\mathrm{f}$ using unfold.

Check back soon!
01:02

Problem 7

Modify the binary string transmitter example to detect simple transmission errors using the concept of parity bits. That is, each eight-bit binary number produced during encoding is extended with a parity bit, set to one if the number contains an odd number of ones, and to zero otherwise. In turn, each resulting nine-bit binary number consumed during decoding is checked to ensure that its parity bit is correct, with the parity bit being discarded if this is the case, and a parity error being reported otherwise. Hint: the library function error $::$ string $\rightarrow$ a displays the given string as an error message and terminates the program; the polymorphic result type ensures that error can be used in any context.

Hast Aggarwal
Hast Aggarwal
Numerade Educator
04:17

Problem 8

Test your new string transmitter program from the previous exercise using a faulty communication channel that forgets the first bit, which can be modelled using the tail function on lists of bits.

Chris Trentman
Chris Trentman
Numerade Educator
03:24

Problem 9

Define a function altMap $::(\mathrm{a} \rightarrow \mathrm{b}) \rightarrow(\mathrm{a} \rightarrow \mathrm{b})$
$\rightarrow[a]-[b]$ that alternately applies its two argument functions to successive elements in a list, in turn about order. For example:
$$
\begin{aligned}
& >\text { altmap }(+10)(+100) \quad[0,1,2,3,4] \\
& {[10,101,12,103,14]}
\end{aligned}
$$

Abby Kennedy
Abby Kennedy
Numerade Educator

Problem 10

Using altmap, define a function luhn :: [Int ] $\rightarrow$ Bool that implements the Luhn algorithm from the exercises in chapter 4 for bank card numbers of any length. Test your new function using your own bank card. Solutions to exercises 1–5 are given in appendix A.

Check back soon!