File tree Expand file tree Collapse file tree 1 file changed +12
-0
lines changed Expand file tree Collapse file tree 1 file changed +12
-0
lines changed Original file line number Diff line number Diff line change @@ -313,6 +313,18 @@ levels t =
313
313
--
314
314
-- > foldTree (\x xs -> maximum (x:xs)) (Node 1 [Node 2 [], Node 3 []]) == 3
315
315
--
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
+ --
316
328
--
317
329
-- @since 0.5.8
318
330
foldTree :: (a -> [b ] -> b ) -> Tree a -> b
You can’t perform that action at this time.
0 commit comments