Haskell - built in tuple with Ord defined to compare by first element only?

Viewed 99

I am relatively new to Haskell, so hopefully this is not a dumb question. I'm looking for a common / well known type that essentially acts a tuple, but has Ord defined such that it only compares by the first element.

I can define this myself as such:

data RankedItem a = RankedItem Double a deriving (Show)

instance Eq (RankedItem a) where
  (RankedItem lkey _) == (RankedItem rkey _) = lkey == rkey

instance Ord (RankedItem a) where
  (RankedItem lkey _) `compare` (RankedItem rkey _) = compare lkey rkey

Is there a built in type or a commonly used third party container that achieves this?

1 Answers

Yes, such a type exists. It's the Arg type defined in Data.Semigroup. Its Eq and Ord instances only compare based off the first element, and it has various other instances (like Functor) that act off the second element.

Related