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 induction case
general case and specific case
base case and general 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 non-matching case should operate on xs, not on (x:xs)
The recursion for the matching case should work on (x:xs), not on xs
The base case is wrong
3. What is the effect of the following fold?
foldl (\acc elt -> acc++[elt]) "" "A string"
It will return the string except its last character
It will return “A stringâ€
It will return “gnirts Aâ€
4. What is the wrong with the following map/fold-based computation?
foldl (+) (map (*2) [1..8])
The map and foldl functions should be swapped
map should take a function like (*), not (*2)
foldl needs an accumulator argument.
5. What is the result of the following computation?
foldr (/) 1 [2,4,8]
2.0
0.25
4.0
0.5
6. What is the result of the following computation?
foldl (/) 16 [8,4,2,1]
0.25
2.0
0.5
4.0
Submit Quiz