[Haskell-cafe] groupBy without Ord?

Mateusz Kowalczyk fuuzetsu at fuuzetsu.co.uk
Sat Mar 22 18:39:55 UTC 2014


On 22/03/14 18:29, martin wrote:
> Am 03/22/2014 06:40 PM, schrieb Kim-Ee Yeoh:
>>
>> On Sat, Mar 22, 2014 at 11:51 PM, martin <martin.drautzburg at web.de <mailto:martin.drautzburg at web.de>> wrote:
>>
>>     How can I groupBy a List whose elements are only instances of Eq but not of Ord?
>>
>>
>> If you take a look at the code for groupBy:
>>
>> groupBy           :: (a -> a -> Bool) -> [a] -> [[a]]
>> groupBy _  []     =  []
>> groupBy eq (x:xs) =  (x:ys) : groupBy eq zs
> 
> Cool!
> 
> I have a database background and the SQL "group by" does of course not assume any ordering. So I often wonder, where you
> would use Haskell's groupBy WITHOUT sorting first, but I assume there are situations, where this is useful.
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
> 

One example that comes to mind is filtering consecutive-same elements:

| Prelude Data.List> map head $ group "hello world!!!"
| "helo world!"

-- 
Mateusz K.


More information about the Haskell-Cafe mailing list