[Haskell-beginners] How to define ord manually.

Chaddaï Fouché chaddai.fouche at gmail.com
Thu Jul 30 14:42:43 UTC 2015


While you may define this *Ord* instance by defining the *compare* method,
you should probably not do this since *Ord* is supposed to follow some
rules, in particular transitivity "*a < b and b < c means a < c*" is
assumed but false in your case. Those rules are important for many
functions to work correctly (sort, minimum, maximum and so on...).

You may simply define another function like *winAgainst* :


*Rock `winAgainst` Scissors = True*

*Scissors `winAgainst` Paper = True*


*Paper `winAgainst` Rock = True_ `winAgainst` _ = False*
or maybe a *battle* function that returns a *Win | Loss | Tie*...

(NB : All functions can be used as infix by surrounding their name with `
(antiquotes))

Le jeu. 30 juil. 2015 à 15:34, Nikita Danilenko <nda at informatik.uni-kiel.de>
a écrit :

> Hi Derek,
>
> you can find the desired functions and type classes using Hoogle [1].
> Your attempt at an Ord instance is basically the right idea, but you
> need to define the function "compare" only [2]. From your syntax it
> looks like you are trying to define a function called "ord" that
> additionally uses "compare" (in a way, it cannot be used). Removing
> "ord" in all three cases fixes your problem, although the resulting
> function "compare" is not total.
>
> The documentation of the type classes usually contains a description of
> what functions you need to define. The fact that the "Show" type class
> instance can be obtained using a function called "show" is a coincidence.
>
> Best regards,
>
> Nikita
>
> [1] https://www.haskell.org/hoogle/
>
> [2]
> http://hackage.haskell.org/package/base-4.8.1.0/docs/Prelude.html#t:Ord
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150730/e47647ae/attachment-0001.html>


More information about the Beginners mailing list