From 297d798d77bfa80d610548363b56f16c5bdd4714 Mon Sep 17 00:00:00 2001
From: Mark Seemann
Date: Wed, 22 Aug 2018 16:50:58 +0200
Subject: [PATCH] Merge pull request #559 from ploeh/patch-2
Add examples for foldTree
---
Data/Tree.hs | 12 ++++++++++++
1 file changed, 12 insertions(+)
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
--
1.9.1