[Haskell-cafe] FW: Why does this Ord-class instance crash?
Daniel Fischer
daniel.is.fischer at web.de
Fri May 21 13:34:12 EDT 2010
On Friday 21 May 2010 19:06:51, R J wrote:
> Why does the following, trivial code snippet below hang GHCi when I
> type"Scalene > Failure", and what's the fix?
For an Ord instance, you need to define at least one of compare and (<=) or
the other functions from the class won't work.
All methods have default implementations in terms of compare, compare has a
default implementation in terms of (<=).
If you only implement (<) and then query (>), it's going in circles.
Fixes:
a) implement compare or (<=)
b) choose the correct order for the constructors and add Ord to the
deriving clause.
>
> data Triangle = Failure
> | Equilateral | Isosceles
> | Scalene deriving (Eq,
> Show) instance Ord Triangle where Failure < Failure = False
> Failure < _ = True Equilateral < Failure = False
> Equilateral < Equilateral = False Equilateral < _ = True
> Isosceles < Scalene = True Isosceles < _ = False
> Scalene < _ = False
Your newlines never make it to my mail programme, is your mail programme
configured to send only '\r' and not '\n' ?
>
> (I tried submitting this to beginners at haskell.org, but even though I've
> signed up for that mailing list, I got a bounce-back saying that I
> needed admin approval to submit anything to that list, and I haven't
> heard from an admin, so I'm posting it here.)
If you sent very shortly after subscribing, your subscription may not have
been registered by the watchdog. If you continue to have problems you
should contact the list manager.
More information about the Haskell-Cafe
mailing list