Removes entries from this item in the list of lists.

I have this function that removes the occurrences of this element in the list of lists.

remove          :: Eq a => a -> [[a]] -> [[a]]
remove    y []  = error "Can't remove an element from an empty list"
remove    y xs  = map (filter(/=y)) xs

How could I do the same using List comprehension

thank

+3
source share
2 answers

For each lin xsadd filter (/= xs) lto the result list:

remove y xs = [filter (/= y) l | l <- xs]

or by deleting the filter nested in it. For each xsin xssand for each xin, xshold xonly if it differs from y:

remove y xss = [ [x| x <- xs, x /= y] | xs <- xss]

It’s good if you just practice, but your version with is mapbetter :)

+5
source

I am assuming something like:

 remove y ls = [f|l <- ls, let f = filter (/= y) l]

should be good.

, l, ls, f .

+2

All Articles