为什么用sml中的`let`替换`in`?

时间:2016-12-23 13:47:24

标签: sml smlnj

我有一个local块,只有很少的辅助方法。之后,出现一个主要功能(在inend块之间):

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

1 个答案:

答案 0 :(得分:1)

SML / NJ的解析器错误有点奇怪。当它说“用LET重新调整IN”时它意味着它在第58行的开头看到了令牌“IN”(即关键字“in”),但它因为无法解决而无法解析IN的用处是什么。在这样的情况下,它假装你写了不同的东西来执行错误恢复,我想基于一些硬编码规则。规则不是为修复程序而设计的,只是为了允许解析继续进行,以便在一次编译尝试中可以看到多个解析错误。在这种情况下,它只是说它假装看到“LET”(即关键字“let”)而不是“IN”,然后继续尝试解析。根据我的经验,正确的继续方法是查看第一个解析错误的位置,修复它并重新编译。后来的错误可能非常令人困惑,它关于它如何尝试恢复的信息通常是无用的。