Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/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. "Delegating" class instance definition (Nicolaas du Preez)
2. Re: "Delegating" class instance definition (Brandon Allbery)
3. Re: "Delegating" class instance definition (Nadir Sampaoli)
4. Re: "Delegating" class instance definition (Nicolaas du Preez)
5. Re: "Delegating" class instance definition (Nadir Sampaoli)
----------------------------------------------------------------------
Message: 1
Date: Sat, 11 Oct 2014 15:02:56 +0200
From: Nicolaas du Preez <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] "Delegating" class instance definition
Message-ID: <[email protected]>
Content-Type: text/plain; charset=windows-1252
data Cycle = Cycle { name :: String, size :: Double } deriving (Eq, Show)
instance Ord Cycle where
(<) a b = (<) (size a) (size b) -- problem statement!
?
How can I specify that, on the right-hand side of the problem statement, I
refer to (<) :: Double -> Double -> Bool instead of (<) :: Cycle -> Cycle ->
Bool?
Is there another way to state that "Cycle is an instance of Ord based on size"?
------------------------------
Message: 2
Date: Sat, 11 Oct 2014 09:39:48 -0400
From: Brandon Allbery <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] "Delegating" class instance
definition
Message-ID:
<CAKFCL4XLo7LDJSLt1XMSWgtgoqTyf0PT0BjsXP6J7x_917=i...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Sat, Oct 11, 2014 at 9:02 AM, Nicolaas du Preez <[email protected]>
wrote:
> data Cycle = Cycle { name :: String, size :: Double } deriving (Eq, Show)
>
> instance Ord Cycle where
> (<) a b = (<) (size a) (size b) -- problem statement!
> ?
>
> How can I specify that, on the right-hand side of the problem statement, I
> refer to (<) :: Double -> Double -> Bool instead of (<) :: Cycle -> Cycle
> -> Bool?
>
That should be automatic, since it knows that size :: Cycle -> Double and
therefore should use the Double instance. Can you provide a minimal full
example and full error message?
--
brandon s allbery kf8nh sine nomine associates
[email protected] [email protected]
unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20141011/d5f847bf/attachment-0001.html>
------------------------------
Message: 3
Date: Sat, 11 Oct 2014 16:04:28 +0200
From: Nadir Sampaoli <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] "Delegating" class instance
definition
Message-ID:
<CAFYwTdQA0QBv5QjtbWr+o=KLo=L=ixfkmv4dhwyfkvrvuwj...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
"Nicolaas du Preez":
>
> data Cycle = Cycle { name :: String, size :: Double } deriving (Eq, Show)
>
> instance Ord Cycle where
> (<) a b = (<) (size a) (size b) -- problem statement!
> ?
>
> How can I specify that, on the right-hand side of the problem statement,
I refer to (<) :: Double -> Double -> Bool instead of (<) :: Cycle -> Cycle
-> Bool?
>
> Is there another way to state that "Cycle is an instance of Ord based on
size"?
>
There is the `on` function in Data.Function which allows you to express
such correlation.
On a side note, you probably just want to implement `compare` so all the
other functions of the Ord type class can be derived from it.
--
Nadir
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20141011/b84a6fe1/attachment-0001.html>
------------------------------
Message: 4
Date: Sat, 11 Oct 2014 19:16:36 +0200
From: Nicolaas du Preez <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] "Delegating" class instance
definition
Message-ID: <[email protected]>
Content-Type: text/plain; charset="windows-1252"
Thanks for the reply - since you mentioned that it should work I figured there
must?ve been a mistake somewhere else.
Looking closer I realised that when I copied & pasted the definition for (<) to
the rest of the functions and the problem actually occurred at
...
max a b = max (size a) (size b)
min a b = min (size a) (size b)
?
which has return type of Double where it should be Cycle.
Regards,
Nicolaas
On 11 Oct 2014, at 15:39, Brandon Allbery <[email protected]> wrote:
> On Sat, Oct 11, 2014 at 9:02 AM, Nicolaas du Preez <[email protected]>
> wrote:
> data Cycle = Cycle { name :: String, size :: Double } deriving (Eq, Show)
>
> instance Ord Cycle where
> (<) a b = (<) (size a) (size b) -- problem statement!
> ?
>
> How can I specify that, on the right-hand side of the problem statement, I
> refer to (<) :: Double -> Double -> Bool instead of (<) :: Cycle -> Cycle ->
> Bool?
>
> That should be automatic, since it knows that size :: Cycle -> Double and
> therefore should use the Double instance. Can you provide a minimal full
> example and full error message?
>
> --
> brandon s allbery kf8nh sine nomine associates
> [email protected] [email protected]
> unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20141011/1f4c0205/attachment-0001.html>
------------------------------
Message: 5
Date: Sun, 12 Oct 2014 01:03:09 +0200
From: Nadir Sampaoli <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] "Delegating" class instance
definition
Message-ID:
<CAFYwTdR0mgUrwgsBv6HwoyfqWGuo3+GHG=ymjgxpthortkk...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
> Looking closer I realised that when I copied & pasted the definition for
(<) to the rest of the functions and the problem actually occurred at
> ...
> max a b = max (size a) (size b)
> min a b = min (size a) (size b)
> ?
> which has return type of Double where it should be Cycle.
To define an Ord instance you only need to implement compare because the
other functions are derivable in terms of it.
If you wany to explicitly implement max, a correct implementation could be:
max a b = if size a < size b then b else a
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20141012/6ba7d755/attachment-0001.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 76, Issue 6
****************************************