I define Tree type as:
data Tree a = Leaf | Node a (Tree a) (Tree a)
deriving (Show, Eq)
Here are a fold function : How to write a function of type a-> b -> b -> b for folding a tree, basically same as what I using.
Now I want to write a function leafCount :: Tree a -> Integer
, using fold and at most one helper function, I think I need to distinct leaf and node in different situation but I'm struggle with doing this, here is my code for now:
leafCount = fold sum (Node a left right)
where
sum left right elem = leafCount left + leafCount right + 1
There are lot of errors in this code now that I cannot figure out at all. Please give me the basic idea and the code that can improve mine.