I am trying to define instances for Functor,Applicative and Monad for the following type:
data BTree a=Leaf a | Node (BTree a) (BTree a) deriving (Eq,Show)
I have tried implementing the Functor instance like this:
instance Functor BTree where
fmap f (Leaf t) =Leaf (f t)
fmap f (Node a b) =Node (f a) (f b)
What did work
fmap f (Node a b)=Node (fmap f a) (fmap f b)
I understand it is not correct since being an instance of functor , the form has to be preserved f a -> f b (in our case Node). and in my implementation you would get f a -> b.
What i don't understand:
Why is it an infinite type? Considering Node (f a )(f b) somewhere down the hierarchy a child Node will be Leafand i will apply f to it.
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)