Created
April 28, 2015 06:29
-
-
Save matsu-chara/84590975ffff07be4d90 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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