In the functional programming language shown, map applies a function to every list item, filter keeps items for which a function returns true, and foldl combines a list from the left using a function and a starting value.
Evaluate each expression. [4]
(i) map (+3) [1, 4, 2]
(ii) filter (>2) [1, 4, 2, 5]
(iii) foldl (+) 0 [1, 2, 3, 4]
(iv) foldl (-) 0 [10, 4, 3]- (i) [4, 7, 5](1)
- (ii) [4, 5](1)
- (iii) 10(1)
- (iv) −17 (((0 − 10) − 4) − 3)(1)
- Answer: (i) [4,7,5] (ii) [4,5] (iii) 10 (iv) −17
- Examiner tip: In (iv) the fold starts with 0 on the left: 0 − 10 first, not 10 − 4.