Can some explain me the error and a possibly a fix :
data Tree a = NULL | Node (Tree a) a (Tree a) deriving Show
instance Functor Tree where
............fmap f NULL = NULL
............fmap f (Node left x right) | (left == NULL) && (right == NULL) = Node left (f x) right
................................................... | (left /= NULL) = Node (fmap f left) x right
................................................... | (right /= NULL) = Node left x (fmap f right)
ghci gives:
Prelude> :load programs\tree.hs
[1 of 1] Compiling Main ( programs\tree.hs, interpreted )
programs\tree.hs:32:78:
Couldn't match type `a' with `b'
`a' is a rigid type variable bound by
the type signature for fmap :: (a -> b) -> Tree a -> Tree b
at programs\tree.hs:31:7
`b' is a rigid type variable bound by
the type signature for fmap :: (a -> b) -> Tree a -> Tree b
at programs\tree.hs:31:7
Expected type: Tree b
Actual type: Tree a
In the first argument of `Node', namely `left'
In the expression: Node left (f x) right
In an equation for `fmap':
fmap f (Node left x right)
| (left == NULL) && (right == NULL) = Node left (f x) right
| (left /= NULL) = Node (fmap f left) x right
| (right /= NULL) = Node left x (fmap f right)
Failed, modules loaded: none.
Regards.
Nishant