• Home
  • Textbooks
  • Programming in Haskell
  • Recursive functions

Programming in Haskell

Graham Hutton

Chapter 6

Recursive functions - all with Video Answers

Educators


Chapter Questions

03:19

Problem 1

How does the recursive version of the factorial function behave if applied to a negative argument, such as $(-1)$ ? Modify the definition to prohibit negative arguments by adding a guard to the recursive case.

Florencia Cuzmar
Florencia Cuzmar
Numerade Educator
02:18

Problem 2

Define a recursive function sumdown $::$ Int $\rightarrow$ Int that returns the sum of the non-negative integers from a given value down to zero. For example, sumdown 3 should return the result $3+2+1+0=6$.

Nick Johnson
Nick Johnson
Numerade Educator
04:24

Problem 3

Define the exponentiation operator $\wedge$ for non-negative integers using the same pattern of recursion as the multiplication operator $*$, and show how the expression $2 \wedge 3$ is evaluated using your definition.

Lucas Gagne
Lucas Gagne
Numerade Educator
02:25

Problem 4

Define a recursive function euclid : : Int $\rightarrow$ Int $\rightarrow$ Int that implements Euclid's algorithm for calculating the greatest common divisor of two non-negative
integers: if the two numbers are equal, this number is the result; otherwise, the smaller number is subtracted from the larger, and the same process is then repeated. For example:
euclid $6 \quad 27$
3

WM
William Mead
Numerade Educator
01:06

Problem 5

Using the recursive definitions given in this chapter, show how length $[1,2,3]$, drop $3[1,2,3,4,5]$, and init $[1,2,3]$ are evaluated.

Carson Merrill
Carson Merrill
Numerade Educator

Problem 6

Without looking at the definitions from the standard prelude, define the following library functions on lists using recursion.
a. Decide if all logical values in a list are True:
$$
\text { and :: [Bool] } \rightarrow \text { Bool }
$$
b. Concatenate a list of lists:
$$
\text { concat :: [[a]] } \rightarrow[a]
$$
c. Produce a list with $n$ identical elements:
$$
\text { replicate :: Int } \rightarrow \text { a } \rightarrow \text { [a] }
$$
d. Select the nth element of a list:
$$
\text { (!!) :: [a] } \rightarrow \text { Int } \rightarrow a
$$
e. Decide if a value is an element of a list:
$$
\text { elem :: Eq a } \Rightarrow a \rightarrow[a] \rightarrow \text { Bool }
$$
Note: most of these functions are defined in the prelude using other library functions rather than using explicit recursion, and are generic functions rather than being specific to the type of lists.

Check back soon!
03:08

Problem 7

Define a recursive function merge :: ord a $\Rightarrow[a]$ $>$ [a] $\rightarrow$ [a] that merges two sorted lists to give a single sorted list. For example:
$>$ merge $[2,5,6] \quad[1,3,4]$
$[1,2,3,4,5,6]$
Note: your definition should not use other functions on sorted lists such as insert or isort, but should be defined using explicit recursion.

Morgan Cheatham
Morgan Cheatham
Numerade Educator
03:08

Problem 8

Using merge, define a function msort :: ord a $\Rightarrow$ [a] $\rightarrow$ [a] that implements merge sort, in which the empty list and singleton lists are already sorted, and any other list is sorted by merging together the two lists that result from sorting the two halves of the list separately.

Morgan Cheatham
Morgan Cheatham
Numerade Educator
08:13

Problem 9

Using the five-step process, construct the library functions that:
a. calculate the sum of a list of numbers;
b. take a given number of elements from the start of a list;
c. select the last element of a non-empty list.
Solutions to exercises 1-4 are given in appendix A.

Willis James
Willis James
Numerade Educator