I\'m pretty new to Haskell and I\'m trying to work out how to traverse a n-ary tree. As output I\'m looking to get a list of Leaf values (as the branches have no value), so for
Traversing a tree means traversing all subtrees and flattening the resulting lists into one.
This translates to
travTree (Branch branches) = concat $ map travTree branches
Note that there are even more concise notations like branches >>= travTree
or concatMap travTree branches
for the right hand side of this definition, but I consider the above one to be the clearest.
Edit: Reintroducing the list-comprehension version for the sake of completeness:
travTree (Branch branches) = [ elem | br <- branches, elem <- travTree br ]