[Haskell-beginners] "Delegating" class instance definition

Nadir Sampaoli nadirsampaoli at gmail.com
Sat Oct 11 14:04:28 UTC 2014


"Nicolaas du Preez":
>
> data Cycle = Cycle { name :: String, size :: Double } deriving (Eq, Show)
>
> instance Ord Cycle where
>         (<) a b = (<) (size a) (size b) -- problem statement!
>>
> How can I specify that, on the right-hand side of the problem statement,
I refer to (<) :: Double -> Double -> Bool instead of (<) :: Cycle -> Cycle
-> Bool?
>
> Is there another way to state that "Cycle is an instance of Ord based on
size"?
>

There is the `on` function in Data.Function which allows you to express
such correlation.

On a side note, you probably just want to implement `compare` so all the
other functions of the Ord type class can be derived from it.

--
Nadir
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20141011/b84a6fe1/attachment.html>


More information about the Beginners mailing list