[Haskell] Re: compares :: Ord a => a -> a -> Ordering -> Ordering

Chris Kuklewicz haskell at list.mightyreason.com
Wed Feb 15 07:48:47 EST 2006


Christian Maeder wrote:
> Ben Rudiak-Gould wrote:
>> I just realized that the class Ord should have an additional method:
>>
>>   class Eq a => Ord a where
>>     compares :: a -> a -> Ordering -> Ordering
>>     compares x y d = case compare x y of { EQ -> d ; o -> o }
>>     ...
> 
> How about:
> 
> instance (Ord a, Ord b, Ord c, Ord d) => Ord (a,b,c,d) where
>     compare (a1,b1,c1,d1) (a2,b2,c2,d2) =
>         compare ((a1,b1,c1),d1) ((a2,b2,c2),d2)
> 
> or another tuple nesting)
> 
> Christian

That works, but...

Constructing the new tuples is usually more heap allocation, and these short
lived data items can make the garbage collection load higher.

The `mappend` method avoids this pitfall.


More information about the Haskell mailing list