[Haskell-beginners] Functor on Tree data constructor

Nishant nishantgeek at gmail.com
Thu Apr 3 08:50:20 UTC 2014


Thanks Vlatko ...

That analogy helped .....  I created my own functor typeclass where i
defined fmap :: MyFucntor f => (a->a) -> f a ->f a and that worked ...


On Wed, Apr 2, 2014 at 7:24 PM, Vlatko Basic <vlatko.basic at gmail.com> wrote:

>  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 at gmail.com> <nishantgeek at gmail.com>
> To: beginners at 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 at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>


-- 
Nishant
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140403/33d144af/attachment.html>


More information about the Beginners mailing list