【发布时间】:2017-05-09 05:30:00
【问题描述】:
我有一个 local 块,它的辅助方法很少。之后是一个主函数(在in 和end 块之间):
datatype color = BLACK | RED;
datatype 'a RBTree = Nil
| Br of (int * 'a * color) * 'a RBTree * 'a RBTree;
datatype Balance = RR | LR | LL | RL;
exception NotFound;
local
fun max (num1, num2) ...
fun get_hight ...
fun get_balance_factor ...
fun LL_rotate ...
fun LR_rotate ...
fun RR_rotate ...
fun RL_rotate ...
fun balance_tree (Nil) = (Nil)
| balance_tree (Br(node, Nil, Nil)) = (Br(node, Nil, Nil))
| balance_tree (Br(node, left, right)) =
if (get_balance_factor (Br(node, left, right))) = 2 then
if (get_balance_factor left) = ~1 then (* LR *)
LR_rotate (Br(node, left, right))
else if (get_balance_factor left) > ~1 then (* LL *)
LL_rotate (Br(node, left, right))
else if (get_balance_factor Br(node, left, right)) = ~2 then
if (get_balance_factor right) = 1 then (* RL *)
RL_rotate (Br(node, left, right))
else if (get_balance_factor right) < 1 then (* RR *)
RR_rotate (Br(node, left, right))
else (Br(node, left, right))
in
fun insert ((Nil), item) = Br(item, (Nil), (Nil) )
| insert ( (Br(node, left, right)), item) =
if (#1(node) = #1(node)) then
(Br(item, left, right))
else if (#1(node) < #1(node)) then
balance_tree (Br(node, insert(left, item), right))
else
balance_tree (Br(node, left, insert(right, item)))
end;
... 代表实现。
而insert 是“主要”功能。
SML 给了我这个输出:
- use "ex4.sml";
[opening ex4.sml]
datatype color = BLACK | RED
datatype 'a RBTree = Br of (int * 'a * color) * 'a RBTree * 'a RBTree | Nil
datatype Balance = LL | LR | RL | RR
exception NotFound
ex4.sml:58.1-58.3 Error: syntax error: replacing IN with LET
ex4.sml:69.1 Error: syntax error found at END
uncaught exception Compile [Compile: "syntax error"]
raised at: ../compiler/Parse/main/smlfile.sml:15.24-15.46
../compiler/TopLevel/interact/evalloop.sml:44.55
../compiler/TopLevel/interact/evalloop.sml:296.17-296.20
我不明白为什么要把in 替换为let?
【问题讨论】:
-
该错误消息不是建议——它表明编译器对您的代码感到困惑。某些东西导致它推断您正在尝试
let ... in构造,但它发现in在它期望let的地方 - 或类似的东西。编译器语法错误消息通常是难以理解的。实际问题可能隐藏在fun balance_tree ...的省略号中。或许您应该展示更多相关代码。 -
@JohnColeman 我为
balance_tree添加了代码,我认为你是对的,因为当我注释掉这个函数时,代码似乎没问题,但我找不到它有什么问题 -
您似乎没有足够的
else子句。你有 6 个if,6 个then,但只有 4 个else。 -
@JohnColeman,你是对的!我在两个 [内部]
else if之后添加了else,它解决了我的问题。我还是不明白为什么这会让口译员期待“让”? -
它在
then子句的中间看到了一个in——只有在同一个子句中有一个匹配的let时才有意义。