Some background: I have a foldT function (like foldr but for trees) of the following type in Haskell.
foldT :: (a -> b -> b -> b) -> b -> Tree a -> b
This foldT only takes type (a -> b -> b -> b) as an input function.
I am trying to find a way to convert my tree into a list, and cannot figure out a way to make my append function take the form (a -> b -> b -> b).
The following is ineffective because it is not the correct type:
append x y z = append x:y:z
Any help would be appreciated.
Thank you!
If you want to convert the tree onto a list then the function is relatively easy, so basically if you have tree structure such as:
Then you'd have to extract the value from the tree and add the first value onto an empty list, then apply the same method to the other branches of the tree recursively with the help of the list (++) operator to append all results onto a single list, i.e.:
then to fold it, first:
The function signature is missing an argument here lets look at the signature: a(first argument) -> b (second) -> b (third) -> b (return type) the function takes 3 arguments/parameters but the signature you provided only has 1 b type (which im guessing is your accumulator), so basically this would be the function signature you're looking for:
Now we can work with this signature, then let's move onto the function:
Although I don't think I should be giving you the answer and you should practice a bit more with recursive data structures so you can better understand how to traverse through them.
PS: If you're going to down vote me, then at least explain why!
Yuck, why are you writing code? GHC can automatically fill in an instance of the
Foldable
class, which contains thetoList
you're looking for.This definition of
treeToList
will perform a pre-order traversal because of the order of the fields of theNode
constructor.As you haven't posted it, I will assume your tree is...
... and that the
a -> b -> b -> b
argument tofoldT
takes the fields of theNode
constructor in the same order they were declared.Let's begin tackling this by following the types:
You want to flatten it into a list, so the result type of your function must be
[a]
:That give us a good idea on how to continue:
With:
Now, onward to the arguments.
z
is what will be inserted in lieu of the valuelessLeaf
s, and so it must be[]
. As forf
, it must combine the sublists created from the left and right branches with the value directly in theNode
. Assuming an in-order traversal, we have:Or, without mentioning
t
:And that's it. One caveat is that repeatedly using left-nested
(++)
(which will happen in this case, asfoldT
walks down the branches recursively) can get quite inefficient. In a situation in which you would care about performance it would be worth considering alternative ways of implementing the concatenating function, such as difference lists.P.S.: A note about terminology. Saying that a function is "like foldr but for trees" is ambiguous, as there are two well-known kinds of functions analogous to
foldr
. First, you have the methods of theFoldable
class (cf. Benjamin Hodgson's answer), which flatten the tree into a list as they fold it, no matter what you do. Then there are the more powerful catamorphisms, such as thefoldT
you are using, which are able to make use of the tree structure.The first thing you start with every time is just to start matching patterns :) At that point, it's pretty much mechanical!
Assuming you have:
Let's start matching patterns! :)
What's the first
Tree
constructor? It'sLeaf
!What could we put there? We need something of type
b
...and we only have one one way to get it. by usingz
:)And so, mechanically, we can move on to the next possible pattern:
Well, we can just make it
z
, but that's a little boring. We probably want to uset1
,x
, andt2
. We can usef
, which needs something of typea
...and we havex :: a
:)f
has two more arguments, of typeb
, so what do you think we could put there? We can putz
twice, but that's a little boring. How can we getb
's fromt1
andt2
?