程序结束时的语法错误

时间:2018-03-15 05:10:30

标签: syntax-error ocaml

我正在尝试编译此代码,它在最后一行返回语法错误:(,有任何想法为什么

let nth_index str c n = 
let i = 0 in 
    let rec loop n i=
    let j = String.index_from str i c in
        if n>1 then loop (n-1) j 
        else    
            j

2 个答案:

答案 0 :(得分:3)

您最外面的let不需要匹配in。它是一个顶级的定义。

但是,其他三个let确实需要匹配in。而且你只有两个in

答案 1 :(得分:2)

所以,根据@ Jeffrey的回复,你可以写:

let nth_index str c n = 
    let rec loop n i =
        let j = String.index_from str i c in
        if n>1 then loop (n-1) j else j
    in loop n 0

但是看看你的算法,可能你想在倒数第二行写if n>1 then loop (n-1) (j+1) else j。如果是,请注意此方法可能引发异常,因此良好的做法是将其命名为nth_index_exn

相关问题