
Is there a library function that will create two lists from one based on a predicate, one list for all elements that satisfy the predicate and one for all that do not? Don't want to reinvent the wheel. Michael

On Sun, Mar 14, 2010 at 12:26 PM, michael rice
Is there a library function that will create two lists from one based on a predicate, one list for all elements that satisfy the predicate and one for all that do not? Don't want to reinvent the wheel.
Michael
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Data.List.filter will do the trick. Alex

Hi Michael Data.List.partition - from the docs... partition :: (a -> Bool) -> [a] -> ([a], [a]) Source The partition function takes a predicate a list and returns the pair of lists of elements which do and do not satisfy the predicate, respectively; i.e., partition p xs == (filter p xs, filter (not . p) xs)

Most excellent! Thanks.
Michael
--- On Sun, 3/14/10, Stephen Tetley

Most excellent! Thanks. I frequently use Hoogle[1] when I need a function, that I think must exist, but do not know how it is named. Try:
(a -> Bool) -> [a] -> ([a], [a]) With the recipe from [2] you may even invoke hoogle from within ghci: ghci> :hoogle (a -> Bool) -> [a] -> ([a], [a]) Hope that helps. Cheers, Simon [1] http://haskell.org/hoogle/ [2] http://www.haskell.org/haskellwiki/Ghci#Hoogle

Thanks all,
Wouldn't one need to know the order of the arguments?
(a -> Bool) -> [a] -> ([a], [a])
Michael
--- On Sun, 3/14/10, Simon Hengel
Most excellent! Thanks. I frequently use Hoogle[1] when I need a function, that I think must exist, but do not know how it is named. Try:
(a -> Bool) -> [a] -> ([a], [a]) With the recipe from [2] you may even invoke hoogle from within ghci: ghci> :hoogle (a -> Bool) -> [a] -> ([a], [a]) Hope that helps. Cheers, Simon [1] http://haskell.org/hoogle/ [2] http://www.haskell.org/haskellwiki/Ghci#Hoogle

Am Sonntag 14 März 2010 21:19:19 schrieb michael rice:
Thanks all,
Wouldn't one need to know the order of the arguments?
(a -> Bool) -> [a] -> ([a], [a])
hoogle also lists functions with "similar" types, so it probably also would find partition if you searched for a function of type [a] -> (a -> Bool) -> ([a],[a]). In this case, that would be a quite unnatural argument order, but of course there are other cases.

Cool. Will keep that in mind next time I'm looking for an operation but don't know what it's called.
Michael
--- On Sun, 3/14/10, Simon Hengel
Wouldn't one need to know the order of the arguments?
(a -> Bool) -> [a] -> ([a], [a]) Not really, if you try
[a] -> (a -> Bool) -> ([a], [a]) it will yield the exact same list of results. Cheers.
participants (5)
-
Alexander Dunlap
-
Daniel Fischer
-
michael rice
-
Simon Hengel
-
Stephen Tetley