How do I apply applicative to a RoseTree, i.e. return a tree composed of trees created by the successive application of functions to initial nodes. Here's the code that I have written:
{-# LANGUAGE DeriveFunctor, InstanceSigs #-}
data RoseTree a = Nil | Node a [RoseTree a] deriving(Functor,Show)
instance Applicative RoseTree where
pure :: a -> RoseTree a
pure x = Node x []
(<*>) :: RoseTree (a -> b) -> RoseTree a -> RoseTree b
(<*>) _ Nil = Nil
(<*>) Nil _ = Nil
(<*>) (Node f tree) (Node x subtrees) = Node (f x) (zipWith (<*>) tree subtrees)
I am unsure what's wrong with my definition of pure and (<*>). Here's the error I got:
Error:
failure in expression `(Node (+1) []) <*> (Node 7 [Node 1 [], Node 2 [], Node 3 [Node 4 []]])'
expected: Node 8 [Node 2 [],Node 3 [],Node 4 [Node 5 []]]
but got: Node 8 []
Test cases for reference:
-- >>> (Node (+1) [Node (*2) []]) <*> Nil
-- Nil
--
-- >>> Nil <*> (Node 7 [Node 1 [], Node 2 [], Node 3 [Node 4 []]])
-- Nil
--
-- >>> (Node (+1) []) <*> (Node 7 [Node 1 [], Node 2 [], Node 3 [Node 4 []]])
-- Node 8 [Node 2 [],Node 3 [],Node 4 [Node 5 []]]
--
-- >>> (Node (+1) [Node (*2) []]) <*> (Node 5 [Node 2 [], Node 8 [Node 1 []]])
-- Node 6 [Node 3 [],Node 9 [Node 2 []],Node 10 [Node 4 [],Node 16 [Node 2 []]]]