【发布时间】:2016-02-09 15:56:51
【问题描述】:
我是 SML 的新手。我正在尝试检查二叉树中是否存在给定值。下面是代码的sn-p。执行后它会给出
Warning : match nonexhaustive (n,Node (t1, j, t2)) => ...
我不明白为什么会这样显示。我想我已经涵盖了所有可能的情况。谁能给我提示或链接,这将有助于删除此警告。
datatype inttree = Empty | Node of inttree * int * inttree;
(*find(n,K) here n is the key that we have to find in inttree K*)
val rec find = fn(n, Node(t1,j,t2)) =>
let
val t = Node(t1, j, t2)
val compare = fn(i,j) => i = j
val find' =
fn (n,Empty) => false (* if we have reached the empty node then we are not able to find the key therefore return false *)
| (n,Node(t1,j,t2)) =>
if compare(n,j)
then true (* if value n and j are equal we have found the key n in the tree*)
else find(n,t1) orelse find(n,t2) (* if the value is not equal check in left subtree if found return true else check in the right subtree*)
in
find'(n,t)
end;
【问题讨论】:
-
你永远不会定义
find (n,Empty)我不太确定你在用所有这些匿名函数和compare做什么。你让它变得比需要的复杂得多。 -
@John Coleman 如果 cmets 有帮助?请指导我如何使这不那么复杂。提前致谢