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