这是我的代码:
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))))
然后它告诉我:
Fatal error: exception Match_failure("main.ml", 8, 15)
我不明白这是什么意思,它似乎并没有表明我的错误来自哪里。
此外,我收到以下警告:
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.)
我怎样才能摆脱这个警告?
我的意思是,在我看来,说我错过了这个案例并不意味着什么N(_,_,_)
,但是这个案例总是被处理的,那么为什么编译器告诉我这个案例不匹配呢?