[Haskell-beginners] How to define ord manually.
derek riemer
driemer.riemer at gmail.com
Thu Jul 30 13:16:25 UTC 2015
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
More information about the Beginners
mailing list