Reputation: 369
I'm interested in creating a filter, whose conditions depend on an input list. Specifically, I want to define the filter such that it checks whether a number is divisible by any number in my input list. So for a single element that would be:
my_filter a = filter (\x -> x `mod` a == 0)
basically I want to extend this such that if I have an input list, it would do the following:
my_filter [a,b] = filter (\x -> x `mod` a == 0 || x `mod` b == 0)
How can I define this filter recursively, such that each element of the list creates a new condition?
Upvotes: 1
Views: 301
Reputation: 54078
What you're looking for is the or
function:
my_filter divisors = filter (\x -> or $ map ((0 ==) . (x `mod`)) divisors)
If you wanted to filter where all the values were divisors then you just have to change or
to and
.
Another way to do it would be
my_filter divisors = filter (\x -> any (== 0) $ map (x `mod`) divisors)
These are equivalent functions, just a little different style.
Upvotes: 3