I have the folloing "height" function which returns the height of a tree. When I try to use it, however, I get this exception. How can I fix it? I also have the function "isBalancedTree" which checks if a given tree is balanced.
data Tree = Node Tree Int Tree | Leaf Int deriving Show
height :: Tree -> Integer
height (Node left x right) = 1 + max (height left) (height right)
isBalancedTree :: Tree -> Bool
isBalancedTree (Node left x right) =
let diff = abs (height left - height right) in
diff <= 1 && isBalancedTree left && isBalancedTree right
*Main> height (Node (Node (Leaf 3) 4 (Leaf 2)) 5 (Node (Leaf 4) 7 (Leaf 6)))
*** Exception: Non-exhaustive patterns in function height
isBalancedorheightgets aLeaf?-Wall! The compiler will then point out the missing cases in your pattern matching. Strongly recommended.