Try to visualize with concrete types

data Tree a = NULL | Node (Tree a) a (Tree a) deriving Show

Tree Int      = Node (Tree Int)       Int      (Node Int)       -- OK
Tree String = Node (Tree String) String (Node String)  -- OK

f :: Int -> String
f i = show i

and see what you get:

instance Functor Tree where
  fmap f NULL = NULL
  fmap f (Node left x right)                                                           -- Node (Tree Int)       Int      (Node Int)
    | (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)