Try to visualize with concrete typesTree Int = Node (Tree Int) Int (Node Int) -- OK
data Tree a = NULL | Node (Tree a) a (Tree a) deriving Show
Tree String = Node (Tree String) String (Node String) -- OK
f :: Int -> String
f i = show i
and see what you get:fmap f (Node left x right) -- Node (Tree Int) Int (Node Int)
instance Functor Tree where
fmap f NULL = NULL
| (left == NULL) && (right == NULL) = Node left (f x) right -- Node (Tree Int) String (Node Int) === Not OK
| (left /= NULL) = Node (fmap f left) x right -- Node (Tree String) Int (Node Int) === Not OK
| (right /= NULL) = Node left x (fmap f right) -- Node (Tree Int) Int (Node String) === Not OK
-------- Original Message --------
Subject: [Haskell-beginners] Functor on Tree data constructor
From: Nishant <nishantgeek@gmail.com>
To: beginners@haskell.org
Date: 02.04.2014 12:53
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)
_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners