Skip to content

Commit 297d798

Browse files
ploehtreeowl
authored andcommitted
Merge pull request #559 from ploeh/patch-2
Add examples for foldTree
1 parent 17494c9 commit 297d798

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

Data/Tree.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,18 @@ levels t =
313313
--
314314
-- > foldTree (\x xs -> maximum (x:xs)) (Node 1 [Node 2 [], Node 3 []]) == 3
315315
--
316+
-- Count the number of leaves in the tree:
317+
--
318+
-- > foldTree (\_ xs -> if null xs then 1 else sum xs) (Node 1 [Node 2 [], Node 3 []]) == 2
319+
--
320+
-- Find depth of the tree; i.e. the number of branches from the root of the tree to the furthest leaf:
321+
--
322+
-- > foldTree (\_ xs -> if null xs then 0 else 1 + maximum xs) (Node 1 [Node 2[], Node 3 []]) == 1
323+
--
324+
-- You can even implement traverse using foldTree:
325+
--
326+
-- > traverse' f = foldTree (\x xs -> liftA2 Node (f x) (sequenceA xs))
327+
--
316328
--
317329
-- @since 0.5.8
318330
foldTree :: (a -> [b] -> b) -> Tree a -> b

0 commit comments

Comments
 (0)