Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
sum : Tree Int -> Int
sum tree =
case tree of
Empty -> 0
Node v left right ->
v + sum left + sum right
flatten : Tree a -> List a
flatten tree =
case tree of
Empty -> []
Node v left right -> flatten left ++ [v] ++ flatten right
isElement : a -> Tree a -> Bool
isElement e tree =
case tree of
Empty -> False
Node v left right ->
if e == v then
True
else
(isElement e left) || (isElement e right)
fold : (a -> b -> b) -> b -> Tree a -> b
fold f a tree =
case tree of
Empty -> a
Node v left right ->
fold f (fold f (f v a) left) right
sum' : Tree Int -> Int
sum' tree = fold (+) 0 tree
flatten' : Tree a -> List a
flatten' tree = fold (::) [] tree
isElement' : a -> Tree a -> Bool
isElement' t tree =
fold (\x y -> (x==t) || y) False tree
preorder : Tree a -> List a
preorder tree =
case tree of
Empty -> []
Node v left right ->
[v] ++ preorder left ++ preorder right
inorder : Tree a -> List a
inorder tree =
case tree of
Empty -> []
Node v left right ->
preorder left ++ [v] ++ preorder right
postorder : Tree a -> List a
postorder tree =
case tree of
Empty -> []
Node v left right ->
preorder left ++ preorder right ++ [v]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment