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