compares :: Ord a => a -> a -> Ordering -> Ordering
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 } ... This would make writing Ord instances much easier: instance (Ord a, Ord b, Ord c, Ord d) => Ord (a,b,c,d) where compares (a1,b1,c1,d1) (a2,b2,c2,d2) = compares a1 a2 . compares b1 b2 . compares c1 c2 . compares d1 d2 -- Ben
On 2/14/06, Ben Rudiak-Gould <Benjamin.Rudiak-Gould@cl.cam.ac.uk> 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 } ...
This would make writing Ord instances much easier:
instance (Ord a, Ord b, Ord c, Ord d) => Ord (a,b,c,d) where compares (a1,b1,c1,d1) (a2,b2,c2,d2) = compares a1 a2 . compares b1 b2 . compares c1 c2 . compares d1 d2
I think you meant compare d1 d2 for that last one. Would there be any advantage to this being a member of Ord instead of just a function in the prelude? /g -- We have lingered in the chambers of the sea By sea-girls wreathed with seaweed red and brown Till human voices wake us, and we drown.
On Wed, Feb 15, 2006 at 01:17:43AM +0000, 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 } ...
This would make writing Ord instances much easier:
instance (Ord a, Ord b, Ord c, Ord d) => Ord (a,b,c,d) where compares (a1,b1,c1,d1) (a2,b2,c2,d2) = compares a1 a2 . compares b1 b2 . compares c1 c2 . compares d1 d2
This does the same thing: import Data.Monoid 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 a2 `mappend` compare b1 b2 `mappend` compare c1 c2 `mappend` compare d1 d2
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
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.
participants (5)
-
Ben Rudiak-Gould -
Chris Kuklewicz -
Christian Maeder -
J. Garrett Morris -
Ross Paterson