Trying to use the given fold function to flatten out a tree into a list.
treeFold :: (b -> a -> b -> b) -> b -> Tree a -> b
treeFold _ b Leaf = b
treeFold f b (Node lt x rt) = f (treeFold f b lt) x (treeFold f b rt)
Here is what I have tried to so far:
treeToList :: Tree a -> [a]
treeToList = treeFold (\xs x ys -> xs ++ x : ys) (\x -> [x])
For some reason, I can't quite wrap my head around how to go about doing this? Feels like there's something I haven't quite gotten into my head about Haskell. Any help would be appreciated along with how to go about solving it. Thanks!
Edit:
I realize that the type signature I am using here verges on the nonsensical. In the type signature of treeFold, based on what I can think, the second argument (b) is probably a list since it acts as the accumulator in a way in this case. That would make the third argument (Tree a) a some version of the argument on the left of the equation. Two of the arguments within the function have to be the left and right subtrees within a Node. The third argument is just the value of the Node. Within the function, I need to combine the left tree, right tree and the value in the usual in order fashion but all the different variations I tried have had some issues