How would I go about writing a contains, balanced function given this definition using folds.
data Tree a = Tree a [Tree a]
treeFold :: (a -> [b] -> b) -> Tree a -> b
treeContains :: Eq a => a -> Tree a -> Bool
treeBalanced :: Tree a -> Bool
Note that in the above definition, empty trees are not allowed, and that a leaf is a tree with an empty list of subtrees.
A contains function determines whether the tree contains a given label.
A balanced function determine whether a tree is balanced.
A tree is balanced if the heights of its subtrees different by at most one, and the subtrees are balanced
The contains function that I have got so far is given below.
treeContains :: Eq a => a -> Tree a -> Bool
treeContains a = treeFold (\x y -> if a == x then True else ...)
So how do you do the else part indicate by ...
Any help would be greatly appreciated.
and
,or
andany
in the prelude. Especially figure out what type of an term you should write in place of...
– aleator