I’m wrote a function that finds the height of a binary tree (i.e. the # of nodes along the longest path from the root to the leaf).
let rec height (t: 'a tree) : int =
begin match t with
| Empty -> 0
| Node (l, v, r) -> 1 + max (height 1) (height r)
end
I believe that my function is correct, but the syntax error that occurs when I run my test says “this function is applied to too many arguments; maybe you forgot a ‘;’.
This is my test case
let test () : bool =
height Node(Node(Empty, 1, Empty), 3, Node(Empty, 2, Node(Empty, 4, Empty))) = 3
;; run_test "Node(Node(Empty, 1, Empty), 3, Node(Empty, 2, Node(Empty, 4, Empty)))" test
What is wrong with my test case?
Thanks!
Consider the expression
In OCaml, adjacent values indicate function application, which is left-associative. So this is the same as:
The following will work:
In other words, you just need to add parentheses.