I'm struggling in finding a solution to my last assignment. I have to implement a function sumSucc that takes an IntTree and returns the sum of the elements of the tree obtained from succTree.
succTree basically returns the successor of a given tree and is defined as follows:
succTree :: IntTree -> IntTree
succTree t = tmap ( 1) t
I know I could easily solve this by doing the following:
sumSucc :: IntTree -> Int
sumSucc (Leaf x) = x 1
sumSucc (Branch (x, xl, xr)) = (x 1) sumSucc xl sumSucc xr
But I have to use the function succTree inside of sumSucc. Thanks in advance.
CodePudding user response:
You can implement another function sumTree, and then sumSucc is equivalent to:
sumSucc :: IntTree -> Int
sumSucc x = sumTree (succTree x)
sumTree :: IntTree -> Int
sumTree (Leaf x) = …
sumTree (Branch (x, xl, xr)) = …
here you thus should implement the … parts of sumTree. I leave this as an exercise.
The sumSucc can be implemented without a parameter, and make use of (.) :: (b -> c) -> (a -> b) -> a -> c:
sumSucc :: IntTree -> Int
sumSucc = sumTree . succTree 