Ocaml错误消息错误:解析错误:[绑定]后的“in”预期(在[expr]中)

时间:2014-02-08 13:40:41

标签: ocaml

我正在尝试编写一个函数来计算n中除数的数量,最多为m个除数(其中current是当前除数,count是到目前为止除数的总数)

我得到错误错误:解析错误:在最后一行代码的[绑定](在[expr]中)之后预期的“in”,但我真的不知道“in”可能是最后一行。我在这里做错了什么?

    (* Counts the number of divisors*)
    let rec count_divisors (n: int) (m: int) (current: int) (count: int): int =
       if count > m || current > n then count
       else  if (n mod current) = 0 then count_divisors n m (current+1) (count+1)
       else (count_divisors n m (current+1) count);;

1 个答案:

答案 0 :(得分:0)

我想你在另一个函数中使用这个函数(因为它有很多参数,我只期望一个参数 - 你要计算其除数的数字),对吗?

如果有,请尝试将代码更改为以下内容:

let num_divisors n =
    let rec count_divisors (n: int) (m: int) (current: int) (count: int): int =
           if count > m || current > n then count
           else  if (n mod current) = 0 then count_divisors n m (current+1) (count+1)
           else (count_divisors n m (current+1) count)
    in count_divisors n n 1 0;;