[Haskell-beginners] Functor on Tree data constructor

Nishant nishantgeek at gmail.com
Wed Apr 2 10:53:33 UTC 2014


Hi,

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140402/97bd8b3b/attachment.html>


More information about the Beginners mailing list