Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Standard name for (filter p xs, filter (not.p) xs)

Is there some function in haskell that evaluates to (filter p xs, filter (not.p) xs) in one list traversal (here are two) or is there some common name in functional programming for this kind of function?

like image 840
Trismegistos Avatar asked Mar 22 '12 14:03

Trismegistos


2 Answers

First look at the type that you need:

Prelude> :t \p xs -> (filter p xs, filter (not . p) xs)
\p xs -> (filter p xs, filter (not . p) xs)
  :: (a -> Bool) -> [a] -> ([a], [a])

Hoogle is your friend:

Prelude> :hoogle (a -> Bool) -> [a] -> ([a], [a])
Prelude break :: (a -> Bool) -> [a] -> ([a], [a])
Prelude span :: (a -> Bool) -> [a] -> ([a], [a])
Data.List break :: (a -> Bool) -> [a] -> ([a], [a])
Data.List partition :: (a -> Bool) -> [a] -> ([a], [a])
Data.List span :: (a -> Bool) -> [a] -> ([a], [a])

Now try out the functions:

Prelude> break odd [1..10]
([],[1,2,3,4,5,6,7,8,9,10])
Prelude> span odd [1..10]
([1],[2,3,4,5,6,7,8,9,10])
Prelude> import Data.List
Prelude Data.List> partition odd [1..10]
([1,3,5,7,9],[2,4,6,8,10])
like image 105
fryguybob Avatar answered Oct 06 '22 00:10

fryguybob


Haskell calls it partition.

like image 40
Matthias Benkard Avatar answered Oct 06 '22 00:10

Matthias Benkard