From: Mark Seemann
Date: Wed, 22 Aug 2018 14:50:58 +0000 (+0200)
Subject: Merge pull request #559 from ploeh/patch-2
X-Git-Url: http://git.haskell.org/packages/containers.git/commitdiff_plain/297d798d77bfa80d610548363b56f16c5bdd4714
Merge pull request #559 from ploeh/patch-2
Add examples for foldTree
---
diff --git a/Data/Tree.hs b/Data/Tree.hs
index 3f3a1bb..fefdfd3 100644
--- a/Data/Tree.hs
+++ b/Data/Tree.hs
@@ -313,6 +313,18 @@ levels t =
--
-- > foldTree (\x xs -> maximum (x:xs)) (Node 1 [Node 2 [], Node 3 []]) == 3
--
+-- Count the number of leaves in the tree:
+--
+-- > foldTree (\_ xs -> if null xs then 1 else sum xs) (Node 1 [Node 2 [], Node 3 []]) == 2
+--
+-- Find depth of the tree; i.e. the number of branches from the root of the tree to the furthest leaf:
+--
+-- > foldTree (\_ xs -> if null xs then 0 else 1 + maximum xs) (Node 1 [Node 2[], Node 3 []]) == 1
+--
+-- You can even implement traverse using foldTree:
+--
+-- > traverse' f = foldTree (\x xs -> liftA2 Node (f x) (sequenceA xs))
+--
--
-- @since 0.5.8
foldTree :: (a -> [b] -> b) -> Tree a -> b