Iterators · Each-Left · Practice Question
Remove Multiple Values from Every Sublist
Question
Given the following list of lists:
l:(1 2 3 4;7 2 4 1;1 3 5 1 8;0 8;8 2 4)Return l with the numbers 2 and 0 removed from all sublists.
Expected result:
1 3 4
7 4 1
1 3 5 1 8
,8
8 4Solution
l except\: (2;0)Explanation
The each-left iterator:
\:pairs a single right argument against every item on the left, one at a time. Here the right argument (2;0) stays fixed while except is applied between it and each sublist of l:
(1 2 3 4) except (2;0)
(7 2 4 1) except (2;0)
(1 3 5 1 8) except (2;0)
(0 8) except (2;0)
(8 2 4) except (2;0)Each sublist has the values 2 and 0 removed independently, leaving the third sublist unchanged (it contains neither) and reducing 0 8 down to a single-item list, ,8.
each-left is the right tool whenever the same right-hand argument needs to be applied against every item of a list on the left.