I try to implement a function (Tree a) -> (Tree a) -> (Tree a). The function should sum the node values and return a tree with the sums. Unfortunatley i got the following error message:
Aufgabe10.hs:4:11: Unexpected type ‘Tree a’ In the class declaration for ‘+’ A class declaration should have form class + a b c where ...
This is my code:
data Tree a = Node a (Tree a) (Tree a)
|Empty
class (+) (Tree a) where
(+) :: (Tree a) (Tree a) -> (Tree a)
instance (Num a) => (+) (Tree a) (Tree a) where
(+) (Node a1 b1 c1) (Node a2 b2 c2) = (Node (a1+a2) ((+) b1 b2) ((+) c1 c2))
(+) Empty (Node a b c) = (Node a b c)
(+) (Node a b c) Empty = (Node a b c)
Ok i changed now the class to Plus and named the function plus, cause i don't want to implement all the numb functions. This is the new code:
data Tree a = Node a (Tree a) (Tree a)
|Empty
class Plus a where
plus:: (Tree a) -> (Tree a) -> (Tree a)
instance (Num a) => Plus (Tree a) where
Plus (Node a1 b1 c1) (Node a2 b2 c2) = (Node (a1+a2) (Plus b1 b2) (Plus c1 c2))
Plus Empty (Node a b c) = (Node a b c)
Plus (Node a b c) Empty = (Node a b c)
I get the following errors:
Aufgabe10.hs:8:9: Pattern bindings (except simple variables) not allowed in instance declarations Plus (Node a1 b1 c1) (Node a2 b2 c2) = (Node (a1 + a2) (Plus b1 b2) (Plus c1 c2))
Aufgabe10.hs:9:9: Pattern bindings (except simple variables) not allowed in instance declarations Plus Empty (Node a b c) = (Node a b c)
Aufgabe10.hs:10:9: Pattern bindings (except simple variables) not allowed in instance declarations Plus (Node a b c) Empty = (Node a b c)
class (+) (Tree a) where
? – jub0bsNum
instance forTree a
. You probably do not want to write a class declaration, here. However, if my guess is correct, you will also have to implement the other methods required for aNum
instance declaration; see hackage.haskell.org/package/base-4.8.0.0/docs/Prelude.html#g:7 – jub0bs(Tree a) (Tree a) -> ...
should also be(Tree a) -> (Tree a) -> ...
– chiinstance (Num a) => (+) (Tree a) (Tree a)
should probably beinstance (Num a) => Num (Tree a)
. – jub0bs