Functional Programming (H) - 5.04 - To Infinity (but not beyond)
1. How do we generate an infinite list of integer 1 values?
repeat 1
take 1
[1..]
[1..1]
2. Which one of the following functions will not loop infinitely, if we evaluate it in ghci?
take 10 [1..]
tail [1..]
length [1..]
3. Given a Tree data type as defined earlier in the course:
data Tree = Leaf | Node Int Tree Tree deriving Show
with Leaf and Node constructors, then how do we define an infinite tree?
mkInfiniteTree = mkInfiniteTree Node 0
mkInfiniteTree = Node 0 (mkInfiniteTree) (mkInfiniteTree)
mkInfiniteTree = Node 0 (mkInfiniteTree) (mkInfiniteTree)
4. Which one of the following expressions generates an infinite list of successive factorial numbers? (Recall that the nth factorial is the product of the first n positive integers.)
facts = [1,2,6,...]
facts = (*) [1..]
facts = map (\x-> (foldr (*) 1 [1..x])) [1..]
5. Does the following expression terminate?
let bot = bot bottomList = repeat bot in length(take 5 bottomList)
no, it loops forever
yes, returning integer value 5
Submit Quiz