Laziness Flashcards

1
Q

What is meant by Haskell is a non-strict, or lazy, language?

A

This means it evaluates expressions only when it needs their results.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does laziness allow?

A

Lazy evaluation allows easy handling of infinite data-structures.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the downsides to laziness

A

sometimes it requires quite a lot of memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Define an infinite list of consecutive integers

A

[1..]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Repeat a set of identical values

A

repeat ‘a’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Name two functions used for dealing with infinite lists

A

take

drop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Does the following expression terminate? What wouuld be the returned value? (give reasons)

let bot = bot
    bottomList = repeat bot
in length(take 5 bottomList)
A

yes and returns the integer value 5

bot is never evaluated, or the full extent of the infinite list, due to lazy evaluation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do we generate an infinite list of integer 1 values?

A

repeat 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which one of the following functions will not loop infinitely, if we evaluate it in ghci?

length [1..]

tail [1..]

take 10 [1..]

A

take 10 [1..]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Given a Tree data type as defined earlier in the course:

data Tree = Leaf | Node Int facts = map (\x-> (foldr (*) 1 [1..x])) [1..]Tree Tree deriving Show
with Leaf and Node constructors, then how do we define an infinite tree?

A

mkInfiniteTree = Node 0 (mkInfiniteTree) (mkInfiniteTree)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Give the expression which generates an infinite list of successive factorial numbers? (Recall that the nth factorial is the product of the first n positive integers.)

A

facts = map (\x-> (foldr (*) 1 [1..x])) [1..]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly