Here is my code:
type 'a tree = Empty | N of 'a * 'a tree * 'a tree
let absolute x =
if x > 0 then x
else -x
let rec node = function
| N(_, Empty, Empty) -> 1
| N(_, g, d) -> 1 + node g + node d
let rec balanced = function
| N(_, Empty, Empty) -> 0
| N(_,g,d) when absolute (node g - node d) > 1 -> 1
| N(_,g,d) when absolute (node g - node d) <= 1 -> balanced g + balanced d
let () = print_int (balanced (N ('x', N ('x', Empty, Empty),
N ('x', N ('x', Empty, Empty), Empty))))
Then it's telling me:
Fatal error: exception Match_failure("main.ml", 8, 15)
I don't understand what does it mean and it doesn't seem to indicate where my mistake comes from.
Moreover I get the following warnings:
File "main.ml", line 8, characters 15-93:
Warning 8: this pattern-matching is not exhaustive.
Here is an example of a case that is not matched:
Empty
File "main.ml", line 12, characters 19-190:
Warning 8: this pattern-matching is not exhaustive.
Here is an example of a case that is not matched:
(N (_, Empty, N (_, _, _))|N (_, N (_, _, _), _)|Empty)
(However, some guarded clause may match this value.)
How can I get rid of this warning?
I mean according to me it doesn't mean anything to say that I am missing the case N(_,_,_)
, but this case is always handled, so why is the compiler telling me that this case is not matched?