Question 1: I have the definition for a tree type:
data Tree a = Node a (Tree a) (Tree a) |Empty
Use the Show typeclass to implement a show function. A tree like Node 10 (Node 20 Empty (Node 6 Empty Empty)) (Node 30 Empty Empty)) should display as (10 (20 x 6) 30) . So, a node with two empty branches should not be parenthesized.
My code is as follows:
data Tree a = Node a (Tree a) (Tree a) |Empty deriving (show)
--input
*Main>>Node 10 (Node 20 Empty (Node 6 Empty Empty)) (Node 30 Empty Empty))
--output
*Main>>Node 10 (Node 20 Empty (Node 6 Empty Empty)) (Node 30 Empty Empty))
Is there any method to change it , let the output is:
(10 (20 x 6) 30)
Question 2:
Use the functor class to define fmap for this type ?
(How can I implement above function use functor?)