views:

179

answers:

3

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)
+4  A: 

You can convert the vector into a tuple:

toTuple :: Vector -> (Double, Double)
toTuple (V x y) = (y, x)

fromTuple :: (Double, Double) -> Vector
fromTuple (y, x) = V x y

Since tuples derive Ord (using lexicographic comparison), they can be inserted to the Set. (Define 2 other functions for x-major ordering.)

KennyTM
+8  A: 

Set requires you to use the default Ord instance of the element type.

If you want to use a different Ord instance, the standard way to do that is to use a custom newtype wrapper and then write an Ord instance for that:

newtype Y = Y { unY :: Vector } deriving Eq
instance Ord Y where compare = comparing ((y . unY) &&& (x . unY))

But since this way of comparing is equivalent to the way binary tuples are compared, KennyTM's solution is the simplest here.

Martijn
Nice. I was just typing in the exact same thing.
jrockway
Ingdas
@Ingdas: Where did you get `Num`? The signature of `comparing` is `Ord a => (b -> a) -> b -> b -> Ordering` (http://hackage.haskell.org/packages/archive/base/latest/doc/html/Data-Ord.html#v:comparing)
KennyTM
A: 

I've made a version of the Set datatype which does not have the Ord context, but instead needs a function of type a -> a -> Ordering passed in wherever a Set is constructed. Could that be useful in your case?

(I'm not sure about the copyright status, it's largely untested and the documentation is not modified, so I'm not just putting it up here...)

yatima2975
Can you send it to me? ingdas (at) gmail (dot) comThat's actually exactly what I needed
Ingdas
-1. By posting this kind of answer, you may help the OP but you don't help the whole community.
jfpoilpret
Good point, jfpoilpret. I'll get to work on getting it in a releasable state on GitHub so everyone can benefit. ETA somewhere next week, I hope.
yatima2975
Have you released your implementation yet?
Ingdas