Tree traversal: Difference between revisions

Content added Content deleted
m (→‎Data.Tree nodes: Replacing <lang> with <syntaxhighlight> manually ... (does automation look feasible ?))
m (→‎Haskell: Reduced `treeLeaves` to a foldTree expression)
Line 5,332: Line 5,332:


{{Trans|Python}}
{{Trans|Python}}
<syntaxhighlight lang=haskell>import Data.Tree (Tree (..), drawForest, drawTree, foldTree)
<syntaxhighlight lang=haskell>import Data.Bool (bool)
import Data.Tree (Tree (..), drawForest, drawTree, foldTree)


---------------------- TREE TRAVERSAL --------------------
---------------------- TREE TRAVERSAL --------------------
Line 5,374: Line 5,375:


treeLeaves :: Tree a -> [a]
treeLeaves :: Tree a -> [a]
treeLeaves = go
treeLeaves = foldTree go
where
where
go (Node x []) = [x]
go x [] = [x]
go (Node _ xs) = xs >>= go
go _ xs = concat xs

--------------------------- TEST -------------------------
--------------------------- TEST -------------------------
tree :: Tree Int
tree :: Tree Int