[Haskell-beginners] How to define ord manually.

Nikita Danilenko nda at informatik.uni-kiel.de
Thu Jul 30 13:34:12 UTC 2015


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

On 30/07/15 15:16, derek riemer wrote:
> Hi,
> I was playing around with haskell, and decided to implement a
> RockPaperScissors type.
> 
> data RPS= Rock | Paper | Scissors
> Then I manually derived show for fun.
> instance Show RPS where
>     show Rock = "rock"
>     show Paper = "paper"
>     show Scissors = "scissors"
> 
> But, I have a problem when defining ord.
> How do I look in the sourcecode for ord?
> In ghci I did
> prelude> :i Ord
> <snip>
>          Ord (a, b, c, d, e, f, g, h, i, j)
>   -- Defined in `GHC.Classes'
> instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g, Ord h,
>           Ord i) =>
>          Ord (a, b, c, d, e, f, g, h, i)
>   -- Defined in `GHC.Classes'
> instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g,
>           Ord h) =>
>          Ord (a, b, c, d, e, f, g, h)
>   -- Defined in `GHC.Classes'
> instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g) =>
>          Ord (a, b, c, d, e, f, g)
>   -- Defined in `GHC.Classes'
> instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f) =>
>          Ord (a, b, c, d, e, f)
>   -- Defined in `GHC.Classes'
> instance (Ord a, Ord b, Ord c, Ord d, Ord e) => Ord (a, b, c, d, e)
>   -- Defined in `GHC.Classes'
> instance (Ord a, Ord b, Ord c, Ord d) => Ord (a, b, c, d)
>   -- Defined in `GHC.Classes'
> instance (Ord a, Ord b, Ord c) => Ord (a, b, c)
>   -- Defined in `GHC.Classes'
> instance (Ord a, Ord b) => Ord (a, b) -- Defined in `GHC.Classes'
> instance Ord () -- Defined in `GHC.Classes'
> Prelude>
> Where is ghc.classes? I looked under the ghc directory and didn't find a
> classes.hs file.
> 
> I tried defining it as such.
> 
> instance Ord RPS where
>     ord Scissors `compare` Rock = LT
>     ord Paper `compare` Scissors = LT
>     ord Rock `compare` Paper = LT
> 
> But this fails with verry little errors, and I really don't know how I
> define an instance of Ord for this.
> I want rock to be less than paper, but greater than scissors,
> scissors to be less than rock but greater than paper,
> and paper to be greater than rock but less than scissors.
> Kind of geeky, but it's one way of learning.
> 
> Also, What is the best way to write useful programs,
> One example I tried is to make a simple guess what I am guessing game,
> but I couldn't make heads or tails of the IO typeclasses, or the
> System.Random module. How can I possibly get random numbers and do
> things on them, and does this mean main must be imperative with a do
> statement?
> Thanks,
> Derek
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


-- 
Dipl.-Math. Nikita Danilenko
Research group:
Computer Aided Program Development
Kiel University
Olshausenstr. 40, D-24098 Kiel
Phone: +49 431 880 7275
URL: https://www.informatik.uni-kiel.de/index.php?id=nikita
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0x76C229F0.asc
Type: application/pgp-keys
Size: 8170 bytes
Desc: not available
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150730/f2e6f09e/attachment.key>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: OpenPGP digital signature
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150730/f2e6f09e/attachment.sig>


More information about the Beginners mailing list