Functional Programming (H) - 3.06 - What Have We Learned About Lists?
1. A recursive function must have at least two cases. What are these called?
base case and general case
general case and specific case
base case and induction case
2. What is wrong with the following definition of filter?
filter :: (a -> Bool) -> [a] -> [a] filter pred [] = [] filter pred (x:xs) | pred x = x : filter pred xs | otherwise = filter pred (x:xs)
The predicate should operate on xs, not on x
The recursion for the matching case should work on (x:xs), not on xs
The base case is wrong
The recursion for the non-matching case should operate on xs, not on (x:xs)
3. What is the effect of the following fold?
foldl (\acc elt -> acc++[elt]) "" "A string"
It will return “gnirts Aâ€
It will return “A stringâ€
It will return the string except its last character
4. What is the wrong with the following map/fold-based computation?
foldl (+) (map (*2) [1..8])
foldl needs an accumulator argument.
map should take a function like (*), not (*2)
The map and foldl functions should be swapped
5. What is the result of the following computation?
foldr (/) 1 [2,4,8]
0.5
0.25
2.0
4.0
6. What is the result of the following computation?
foldl (/) 16 [8,4,2,1]
2.0
0.25
0.5
4.0
Submit Quiz