
1 May
2008
1 May
'08
8:44 a.m.
On Thu, May 1, 2008 at 9:32 AM, Edsko de Vries
So then the question becomes: what *is* the best way to write this function?
I guess it would be simpler to have the counter on the data type and a smart branch constructor:
data Tree = Leaf Integer | Branch Integer Tree Tree
count :: Tree -> Integer count (Leaf _) = 1 count (Branch c _ _) = c
branch :: Tree -> Tree -> Tree branch left right = Branch (count left + count right) left right
gentreeL :: Integer -> Tree gentreeL 0 = Leaf 0 gentreeL n = branch (gentreeL (n-1)) (Leaf 0)
etc. -- Felipe.