filtering a list of elements by a list of filters

and a list of filters such as [(>3), (<4), ...]

The signature is incorrect. This is a list of function a -> Bool, so you should use:

filters :: [a] -> [a -> Bool] -> [a]

This will however not be sufficient. You need to turn this into a filter that checks if all the predicates are satisfied. We can do by using all :: Foldable f => (b -> Bool) -> f b -> Bool. Here the b for the all function is a function a -> Bool, and thus the type of the function (b -> Bool) is (a -> Bool) -> Bool. We do this by calling each function on that element:

filters :: [a] -> [a -> Bool] -> [a]
filters xs fs = filter (\x -> all ($ x) fs) xs

we can further generalize the signature to:

filters :: Foldable f => [a] -> f (a -> Bool) -> [a]
filters xs fs = filter (\x -> all ($ x) fs) xs

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top