Proposal for generalized function partition in List-library
Bernd Holzmüller
holzmueller@ics-ag.de
Thu, 17 May 2001 10:06:55 +0200
Hi all,
knowing that the Haskell library report is currently being rewritten, I
would like to propose a new function for module List that generalizes
the current function partition :: (a -> Bool) -> [a] -> [[a]] in the
following way:
partition :: Eq b => (a -> b) -> [a] -> [[a]]
partition _ [] = []
partition f (a:as) =
let (as',as'') = foldr (select (f a)) ([],[]) as
in (a:as'):partition f as''
where
select b x (ts,fs) | f x == b = (x:ts,fs)
| otherwise = (ts,x:fs)
This partitioning function builds equivalence classes from the list
argument, where each element list within the result list consists of
elements that all map to the same value when applying f to it.
Thus: partition (`div` 2) [1..5] yields [[1],[2,3],[4,5]]
This is much more general than the existing partitioning function and
applicable in many practical cases.
Cheers,
Bernd Holzmüller