[Haskell-beginners] Ord

Chaddaï Fouché chaddai.fouche at gmail.com
Fri Dec 30 17:21:03 CET 2011


2011/12/30 Stanisław Findeisen <stf-list at eisenbits.com>:
>
> How to declare an instance of Ord?
>

You very rarely need to do so... In fact even in the report a lot of
the basic instances are already derived automatically (which you can
do by adding "deriving (Ord)" at the end of your data type definition,
or "deriving instance Ord YourType" on a line by itself). If you do
need to, it could look like that :

>
> instance Ord Bool where
>   compare True True = EQ
>   compare True False = GT
>   compare False True = LT
>   compare False False = EQ
>

defining compare is enough, or you could define just (<=) :

>
> instance Ord Bool where
>   False <= _ = True
>   True <= True = True
>   True <= False = False
>

Note that nothing prevents you from defining both, or even the other
comparison functions if there are performance considerations.

-- 
Jedaï



More information about the Beginners mailing list