Reputation: 19
I' am trying to remove an element from a list of lists, but only if the element is on a list with length 1. For example:
removeElement 1 [[2,3],[1,2],[1]]
[[2,3],[1,2]]
removeElement 2 [[1,2,3,4]]
[[1,2,3,4]]
removeElement 3 [[3],[1,4,5]]
[[1,4,5]]
So far I have this, but I don't know how to continue, or which function use.
removeElement :: (Eq a) => a -> [[a]] -> [[a]]
removeElement a [[]] = []
removeElement a ((x:xs):rs) = if a == x then (xs:rs) else [x] :( removeElement a (xs:rs))
Upvotes: 0
Views: 1160
Reputation: 1687
This is an example of filtering:
removeElement :: Eq a => a -> [[a]] -> [[a]]
removeElement e = filter (/= [e])
Testing your examples with ghci:
> removeElement 1 [[2,3],[1,2],[1]]
[[2,3],[1,2]]
> removeElement 2 [[1,2,3,4]]
[[1,2,3,4]]
> removeElement 3 [[3],[1,4,5]]
[[1,4,5]]
Upvotes: 3