On Mon, 2010-11-22 at 14:48 +0800, Magicloud Magiclouds wrote:
> (+) :: A -> A -> A
> (+) a b =
> A (elem1 a + elem1 b) (elem2 a + elem2 b) -- I got errors here, for
> the (+) is ambiguous.
That's because (+) is implicitly imported from the Prelude. If you
import Prelude hiding ((+))
the
> On 22 November 2010 07:48, Magicloud Magiclouds
> mailto:magicloud.magiclo...@gmail.com>>
> wrote:
>
> Hi,
> For example, I have a data A defined. Then I want to add (+) and (-)
> operators to it, as a sugar (compared to addA/minusA). But * or other
> stuff defined in class Num
Haskell does not play as well with overloading as one would do it in C++;
every
name used must be fully qualified. Indeed, if we try something like
Indeed, if we try something like
data A = A Int deriving (Show, Eq)
test = A 3 unA (A i) = i
class Group a where (+) :: a -> a -> a
instance Grou
Sure, you can define your own type class like that:
import Prelude hiding ((+), (-)) -- usual (+) and (-) shouldn't be here...
import qualified Prelude as P -- but they still are accessible with a prefix
class Group a where
(+) :: a -> a -> a
(-) :: a -> a -> a
instance Group Integer where
Check out the awesome prelude, used to define ESDLs:
http://tom.lokhorst.eu/2010/02/awesomeprelude-presentation-video
On 22 November 2010 07:48, Magicloud Magiclouds <
magicloud.magiclo...@gmail.com> wrote:
> Hi,
> For example, I have a data A defined. Then I want to add (+) and (-)
> operators
On 11/21/10 10:48 PM, Magicloud Magiclouds wrote:
Hi,
For example, I have a data A defined. Then I want to add (+) and (-)
operators to it, as a sugar (compared to addA/minusA). But * or other
stuff defined in class Num is meanless to A. So I just do:
(+) :: A -> A -> A
(+) a b =
A (elem1
Hi,
For example, I have a data A defined. Then I want to add (+) and (-)
operators to it, as a sugar (compared to addA/minusA). But * or other
stuff defined in class Num is meanless to A. So I just do:
(+) :: A -> A -> A
(+) a b =
A (elem1 a + elem1 b) (elem2 a + elem2 b) -- I got errors here,