It could help to take a type-driven approach here. As in, you want to remove all occurrences of some element from a list of list of elements.
This is, “given an a
and a list of list of a's
([[a]]
) return a list of list of a
‘s ([[a]]
):
removeAll :: Eq a => a -> [[a]] -> [[a]]
removeAll v (list:listOfLists) = ???
We’re removing all v
from each list
in the list (list:listOfLists)
. Another way of saying this is “for each list
in our listOfLists
filter out v
“.
So, we want to perform the same action (filter) for-each value in a list. In Haskell, this translates to mapping the filter action over every list
in the listOfLists
.
removeAll :: Eq a => a -> [[a]] -> [[a]]
removeAll v xs = map (filter (/= v)) xs
CLICK HERE to find out more related problems solutions.