Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell multifilter

Tags:

haskell

I want to filter a list by predicates curried from another list. For instance:

multifilter :: (a -> a -> Bool) -> [a] -> [a] -> [a]
multifilter _ _ [] = []
multifilter _ [] _ = []
multifilter f (x:xs) ys = (filter (f x) ys) ++ (multifilter f xs ys)

With usage such as:

prelude> multifilter (==) [1,2,3] [5,3,2]
[2,3]

Is there a standard way to do this?

like image 617
dos Avatar asked Nov 29 '22 14:11

dos


1 Answers

You can use intersectBy:

λ> :t intersectBy
intersectBy :: (a -> a -> Bool) -> [a] -> [a] -> [a]
λ> intersectBy (==) [1,2,3] [5,3,2]
[2,3]

You can use hoogle to search functions using type signature and finding them.

like image 102
Sibi Avatar answered Dec 10 '22 12:12

Sibi