
On 22/03/14 16:51, martin wrote:
Hello all,
when I groupBy a list in order to cluster elements which satisfy some sort of equality, I usually have to sort the list first, which requires Ord. However groupBy itself does not require Ord, but just Eq.
How can I groupBy a List whose elements are only instances of Eq but not of Ord?
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
group(By) is not concerned with grouping elements from the whole list together. It is only concerned about grouping elements that fulfill the equality that are already next to each other. | > group [1, 2, 1] | [[1], [2], [1]] If you want it to group the elements from the whole list, it as as you mention, you have to sort the list first or arrange it otherwise so that equal elements are next to each other. So to answer your question, you can groupBy Eq a => [a] simply by calling groupBy. If you want groupBy to group elements from the whole list, you need to arrange for such elements to be next to each other in the list. The Ord instance is normally used for this. Alternatively you can write a function which will simply go through the list for each distinctive element and collect the groups that way. This is _not_ what groupBy does: you're looking at the wrong function. -- Mateusz K.