Laziness Flashcards
What is meant by Haskell is a non-strict, or lazy, language?
This means it evaluates expressions only when it needs their results.
What does laziness allow?
Lazy evaluation allows easy handling of infinite data-structures.
What are the downsides to laziness
sometimes it requires quite a lot of memory
Define an infinite list of consecutive integers
[1..]
Repeat a set of identical values
repeat ‘a’
Name two functions used for dealing with infinite lists
take
drop
Does the following expression terminate? What wouuld be the returned value? (give reasons)
let bot = bot bottomList = repeat bot in length(take 5 bottomList)
yes and returns the integer value 5
bot is never evaluated, or the full extent of the infinite list, due to lazy evaluation.
How do we generate an infinite list of integer 1 values?
repeat 1
Which one of the following functions will not loop infinitely, if we evaluate it in ghci?
length [1..]
tail [1..]
take 10 [1..]
take 10 [1..]
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?
mkInfiniteTree = Node 0 (mkInfiniteTree) (mkInfiniteTree)
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.)
facts = map (\x-> (foldr (*) 1 [1..x])) [1..]