我正在尝试编写一个函数,该函数可以计算n到m除数中的除数(其中当前是当前除数,计数是到目前为止的除数总数)
我收到错误错误:解析错误:最后一行代码的 [绑定](在 [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);;
我想你在另一个函数中使用这个函数(因为它有这么多参数,我只期望一个参数——你想要计算其除数的数字),对吧?
如果是这样,请尝试将代码更改为以下内容:
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;;