I’m trying to put this data type in a Haskell Set, but I don’t want to give it a general instance of Ord. So I want to give the set an ordering on y-coördinate but without instance Ord Vector. Is this possible?
data Vector = V
{ x :: Double
, y :: Double
} deriving (Eq)
Setrequires you to use the defaultOrdinstance of the element type.If you want to use a different
Ordinstance, the standard way to do that is to use a customnewtypewrapper and then write anOrdinstance for that:But since this way of comparing is equivalent to the way binary tuples are compared, KennyTM’s solution is the simplest here.