Reputation: 13
I need to find an element l in a list of list, removing each list containing the element, and then return the list of list, but i don't know how to parsing list of list and, at the same time, removing the list and keeping parsing the rest of the list.
To be more understandable, i have :
data Object = Pos Char | Neg Char
deriving (Show, Eq)
type Box = [Object]
type Formula = [Box]
findAndDelete :: Formula -> Object -> Formula
findAndDelete cs l
findAndDelete (c:cs) l
edit : At the same time, I need to remove all x element from the Formula,x is returned by a function named negative with l in parameter.
negative :: Object -> Object
negative (Pos v) = Neg v
negative (Neg v) = Pos v
Upvotes: 1
Views: 1192
Reputation: 4491
Since you want to remove the Box
that contains the element you could use elem :: Eq a => a -> [a] -> Bool
to check just that:
findAndDelete :: Formula -> Object -> Formula
findAndDelete [] o = []
findAndDelete (b:bs) o = case (elem o b) of {
True -> findAndDelete bs o;
False -> b:(findAndDelete bs o)
};
So to remove the negative objects during the findAndDelete
function you can use filter :: (a -> Bool) -> [a] -> [a]
this will give you a Box
where all the objects satisfy the predicate:
findAndDelete :: Formula -> Object -> Formula
findAndDelete [] o = []
findAndDelete (b:bs) o = case (elem o b) of {
True -> findAndDelete bs o;
False -> (filter ((/=) (negative o)) b):(findAndDelete bs o)
};
Upvotes: 0
Reputation: 116139
If you want to remove the boxes, you can filter for all the boxes who do not have the given object as their element.
import Data.List
findAndDelete :: Formula -> Object -> Formula
findAndDelete f o = filter (not . elem o) f
Upvotes: 1