Given the following algebraic data type, a Rose Tree:
data Tree a = Node {
rootLabel :: a,
subForest :: [Tree a]
}
I attempted a foldTree
function to fold over this list: (credit to this class's homework from 2013:
treeFold :: (b -> [b] -> b) -> (a -> b) -> Tree a -> b
treeFold f g tree = f (g (rootLabel tree)) (map (g . rootLabel) (subForest tree))
test
*Party> let tree = Node { rootLabel = 100, subForest = [] }
*Party> let tree2 = Node { rootLabel = 200, subForest = [tree] }
*Party> add tree2
300
Please review this implementation.
Given my definition of treeFold
, I don't see how I could fold over a Tree Char
, producing a [Char]/String
result.
My understanding is that, for the return type, b
, it can't be [a]
.