• Home
  • Textbooks
  • Programming in Haskell
  • Lazy evaluation

Programming in Haskell

Graham Hutton

Chapter 15

Lazy evaluation - all with Video Answers

Educators


Chapter Questions

00:21

Problem 1

Identify the redexes in the following expressions, and determine whether each redex is innermost, outermost, neither, or both:
$$1+(2 * 3)$$$$
(1+2) *(2+3)$$
fst $(1+2,2+3)$$$
(\backslash x->1+x)(2 * 3)$$

Julie Silva
Julie Silva
Numerade Educator
01:06

Problem 2

Show why outermost evaluation is preferable to innermost for the purposes of evaluating the expression fst $(1+2,2+3)$.

Micah Hurewitz
Micah Hurewitz
Numerade Educator
00:31

Problem 3

Given the definition mult $=\langle x \rightarrow(\backslash y \rightarrow x * y)$, show how the evaluation of mult 34 can be broken down into four separate steps.

Amy Jiang
Amy Jiang
Numerade Educator
01:05

Problem 4

Using a list comprehension, define an expression fibs $::$ [Integer] that generates the infinite sequence of Fibonacci numbers
$$0,1,1,2,3,5,8,13,21,34, \ldots$$
using the following simple procedure:
- the first two numbers are 0 and 1 ;
- the next is the sum of the previous two;
- return to the second step.
Hint: make use of the library functions zip and tail. Note that numbers in the Fibonacci sequence quickly become large, hence the use of the type Integer of arbitrary-precision integers above.

Nick Johnson
Nick Johnson
Numerade Educator
01:45

Problem 5

Define appropriate versions of the library functions
repeat : : a $->[a]$
repeat $x=x$ where $x s=x: x s$
take : : Int $\rightarrow[a] \rightarrow[a]$
take $0=[]$
take -[]$=[]$
take $n(x: x s)=x:$ take $(n-1) \mathrm{xs}$
replicate : Int $\rightarrow$ a $->$ [a]
replicate $n=$ take $n$ repeat
repeat ::a $\rightarrow[a]$
repeat $\mathrm{x}=\mathrm{xs}$ where $\mathrm{xs}=\mathrm{x}: \mathrm{xs}$
take :: Int $\rightarrow[\mathrm{a}] \rightarrow[a]$
take $0_{-}=[]$
take -[]$=[]$
take $\mathrm{n}(\mathrm{x}: \mathrm{xs})=\mathrm{x}$ : take $(\mathrm{n}-1) \mathrm{xs}$
replicate : : Int $\rightarrow$ a $\rightarrow$ [a]
replicate $n$ = take $n$. repeat
for the following type of binary trees:
data Tree a = Leaf $\mid$ Node (Tree a) a (Tree a) deriving show

James Kiss
James Kiss
Numerade Educator
04:40

Problem 6

Newton's method for computing the square root of a (non-negative) floating-point number $\mathrm{n}$ can be expressed as follows:
- start with an initial approximation to the result;
- given the current approximation a, the next approximation is defined by the function next $a=(a+$ $\mathrm{n} / \mathrm{a}) / 2$;
- repeat the second step until the two most recent approximations are within some desired distance of one another, at which point the most recent value is returned as the result.

Define a function sqroot :: Double $\rightarrow$ Double that implements this procedure. Hint: first produce an infinite list of approximations using the library function iterate. For simplicity, take the number 1.0 as the initial approximation, and 0.00001 as the distance value.
Solutions to exercises 1-3 are given in appendix A.

Sam Sohn
Sam Sohn
Numerade Educator