[Haskell-cafe] PartitionBy (was: Behavior groupBy)

Hans van Thiel hthiel.char at zonnet.nl
Sun Apr 29 09:39:15 EDT 2007


Hello All,

As usual, my first attempt could be better.. 

-- partitions a list according to an equivalence relation

import Data.List (partition)

partitionBy :: (a -> a -> Bool) -> [a] -> [[a]]
partitionBy eq [] = []
partitionBy eq ls = x:(partitionBy eq y)  where
                   (x,y) = partition ((head ls) `eq`) ls 

example>
partitionBy (\x y -> (last x) == (last y)) ["abc","bd","bdc","abd"]
result> 
[["abc","bdc"],["bd","abd"]]

Of course, any crits would be appreciated, but maybe this will save
somebody some time..

Thanks,

Hans van Thiel



More information about the Haskell-Cafe mailing list