Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dvdsgl
Created July 25, 2009 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dvdsgl/155116 to your computer and use it in GitHub Desktop.
Save dvdsgl/155116 to your computer and use it in GitHub Desktop.
data Tree a = Branch (Tree a) (Tree a)
| Leaf a
treeOp :: (a -> b) -> (b -> b -> b) -> Tree a -> b
treeOp fL fB (Leaf a) = fL a
treeop fL fB (Branch t1 t2) = treeOp fL fB t1 `fB` treeOp fL fB t2
myFringe :: Tree a -> [a]
myFringe = treeOp (:[]) (++)
myTreeSize :: Tree a -> Integer
myTreeSize = treeOp (const 1) +
myTreeHeight :: Tree a -> Integer
myTreeHeight = treeOp (const 0) maxPlusOne
where maxPlusOne a b = max a b + 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment