Skip to content

Instantly share code, notes, and snippets.

@matsu-chara
Created April 28, 2015 06:29
Show Gist options
  • Save matsu-chara/84590975ffff07be4d90 to your computer and use it in GitHub Desktop.
Save matsu-chara/84590975ffff07be4d90 to your computer and use it in GitHub Desktop.
data Node = Leaf Integer | Branch Node Node
depth tree = case tree of
Leaf _ -> 1
Branch a b -> 1 + max (depth a) (depth b)
depth_tail tree = depth' tree id
where
depth' (Leaf _) cont = cont 1
depth' (Branch a b) cont =
depth' a (\c ->
depth' b (\d ->
cont(max (c + 1) (d + 1))
)
)
main = do
print $ depth (Branch (Leaf 2) (Leaf 1))
print $ depth_tail (Branch (Leaf 2) (Leaf 1))
print $ depth_tail (Branch (Leaf 2) (Branch (Leaf 2) (Leaf 1)))
print $ depth_tail (Branch (Branch (Leaf 2) (Leaf 1)) (Leaf 1))
print $ depth_tail (Branch (Branch (Leaf 2) (Branch (Branch (Leaf 2) (Leaf 1)) (Leaf 1))) (Leaf 1))
print $ depth (Branch (Branch (Leaf 2) (Branch (Branch (Leaf 2) (Leaf 1)) (Leaf 1))) (Leaf 1))
print $ depth (Branch (Leaf 1) (Branch (Leaf 2) (Branch (Branch (Leaf 2) (Leaf 1)) (Leaf 1))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment