Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  How to define ord manually. (derek riemer)
   2. Re:  How to define ord manually. (Nikita Danilenko)
   3. Re:  How to define ord manually. (Marcin Mrotek)
   4. Re:  How to define ord manually. (Imants Cekusins)
   5. Re:  How to define ord manually. (Chadda? Fouch?)


----------------------------------------------------------------------

Message: 1
Date: Thu, 30 Jul 2015 07:16:25 -0600
From: derek riemer <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] How to define ord manually.
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8; format=flowed

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


------------------------------

Message: 2
Date: Thu, 30 Jul 2015 15:34:12 +0200
From: Nikita Danilenko <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] How to define ord manually.
Message-ID: <[email protected]>
Content-Type: text/plain; charset="windows-1252"

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
> [email protected]
> 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-0001.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-0001.sig>

------------------------------

Message: 3
Date: Thu, 30 Jul 2015 16:38:06 +0300
From: Marcin Mrotek <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] How to define ord manually.
Message-ID:
        <CAJcfPzn+G2=wk0ZTpX1gUW53YxO+Cft=zvnehrzt6xcplff...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

The first Google result:
https://hackage.haskell.org/package/base-4.8.1.0/docs/Data-Ord.html It says
right there that you need to define (at least) either "compare" or "<=",
and that there's no "ord" function.

Best regards,
Marcin Mrotek
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150730/0109b4c4/attachment-0001.html>

------------------------------

Message: 4
Date: Thu, 30 Jul 2015 16:10:47 +0200
From: Imants Cekusins <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] How to define ord manually.
Message-ID:
        <CAP1qinYzmvn2Jhm_3wohoD6VYGQ=seqpge41+l+7he9_xbo...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

Derek, if you face any questions, feel free to write here.

The search engines are there alright but humans do tend to
communicate. Even if it may be in the books somewhere.

Nothing wrong with that ;)


------------------------------

Message: 5
Date: Thu, 30 Jul 2015 14:42:43 +0000
From: Chadda? Fouch? <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] How to define ord manually.
Message-ID:
        <CANfjZRZm7Tbmzzdq_+7CmtvhWfN9hhvUE4x_o-QVw1gXRT34=g...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

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 <[email protected]>
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.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 85, Issue 20
*****************************************

Reply via email to