Unordered map

Christian Maeder Christian.Maeder at dfki.de
Wed Jul 30 11:01:31 EDT 2008


Andrew Wagner wrote:
> Suppose I have some data structure that is inherently unordered, and I
> want to use it as a key into a Map. How would I get around this
> limitation on the client side? Well, the most obvious way is to create
> some nonsensical Ord instance, like compare _ _ = Eq. Is that the
> recommended solution?

instance Ord ... where
    compare _ _ = EQ

is a bad idea, because it makes all elements equal and therefore all 
maps will be empty or singletons. (In order to construct such maps the 
order is not needed.)

The recommended solution is to put "deriving (Eq, Ord)" after your data 
types for keys. (At least an Eq instance is needed for association 
lists, i.e. "Prelude.lookup")

Cheers Christian



More information about the Libraries mailing list