• Home
  • Textbooks
  • Programming in Haskell
  • List comprehensions

Programming in Haskell

Graham Hutton

Chapter 5

List comprehensions - all with Video Answers

Educators


Chapter Questions

01:56

Problem 1

Using a list comprehension, give an expression that calculates the sum $1^2+2^2+\ldots 100^2$ of the first one hundred integer squares.

Amy Jiang
Amy Jiang
Numerade Educator
01:46

Problem 2

Suppose that a coordinate grid of size $m \times n$ is given by the list of all pairs $(x, y)$ of integers such that $0 \leqslant x \leqslant m$ and $0 \leqslant y \leqslant n$. Using a list comprehension, define a function grid $::$ Int $\rightarrow$ Int $\rightarrow$ [(Int, Int)] that returns a coordinate grid of a given size. For example:
$$
\begin{aligned}
& >\text { grid } 12 \\
& {[(0,0),(0,1),(0,2),(1,0),(1,1),(1,2)]}
\end{aligned}
$$

Angelo Rendina
Angelo Rendina
Numerade Educator
01:37

Problem 3

Using a list comprehension and the function grid above, define a function square :: Int $\rightarrow$ [ (Int, Int)] that returns a coordinate square of size $n$, excluding the diagonal from $(0,0)$ to $(n, n)$. For example:
$$
\begin{aligned}
& >\text { square } 2 \\
& {[(0,1),(0,2),(1,0),(1,2),(2,0),(2,1)]}
\end{aligned}
$$

Anthony Ramos
Anthony Ramos
Numerade Educator

Problem 4

In a similar way to the function length, show how the library function replicate :: Int $\rightarrow a \rightarrow$ [a] that produces a list of identical elements can be defined using a list comprehension. For example:
$>$ replicate 3 True

Check back soon!
00:55

Problem 5

A triple $(x, y, z)$ of positive integers is Pythagorean if it satisfies the equation $x^2+y^2=z^2$. Using a list comprehension with three generators, define a function pyths :: Int $\rightarrow$ [(Int, Int, Int)] that returns the list of all such triples whose components are at most a given limit. For example:
$$
\begin{aligned}
& >\text { pyths } 10 \\
& {[(3,4,5),(4,3,5),(6,8,10),(8,6,10)]}
\end{aligned}
$$

Vicki Stebbins
Vicki Stebbins
Numerade Educator
07:24

Problem 6

A positive integer is perfect if it equals the sum of all of its factors, excluding the number itself. Using a list comprehension and the function factors, define a function perfects :: Int $\rightarrow$ [Int] that returns the list of all perfect numbers up to a given limit. For example:
$$
\begin{aligned}
& >\text { perfects } 500 \\
& {[6,28,496]}
\end{aligned}
$$

Darren Wilson
Darren Wilson
Numerade Educator

Problem 7

Show how the list comprehension $[(x, y) \quad \mathrm{x}<-$ $[1,2], y<-[3,4]]$ with two generators can be reexpressed using two comprehensions with single generators. Hint: nest one comprehension within the other and make use of the library function concat :: $[$ [a] ] $\rightarrow[a]$.

Check back soon!
01:26

Problem 8

Redefine the function positions using the function find.

Gregory Higby
Gregory Higby
Numerade Educator
00:51

Problem 9

The scalar product of two lists of integers $x s$ and $y s$ of length $n$ is given by the sum of the products of corresponding integers:
$$
\sum_{i=0}^{n-1}\left(x s_i * y s_i\right)
$$
In a similar manner to chisqr, show how a list comprehension can be used to define a function scalarproduct $::$ [Int] $\rightarrow$ [Int] $\rightarrow$ Int that returns the scalar product of two lists. For example:
$>$ scalarproduct $[1,2,3] \quad[4,5,6]$
32

mp
Manik Pulyani
Numerade Educator
04:57

Problem 10

Modify the Caesar cipher program to also handle uppercase letters.
Solutions to exercises 1-5 are given in appendix A.

WM
William Mead
Numerade Educator