OCaml中的递归函数

una*_*tre 4 stack-overflow recursion ocaml

我有一点问题:我想用OCaml 解决这个问题,所以我尝试了这个 - >

-> let rec somme x = if ( nor (bool_of_int (x mod 3)) (bool_of_int (x mod 5))) then x + (somme x-1) else (somme x-1) ;;

val somme : int -> int = <fun>

-> somme 1000 ;;

Stack overflow during evaluation (looping recursion?).
Run Code Online (Sandbox Code Playgroud)

我做错了什么?


我试过的新代码:

let somme2 x = if (( nor (bool_of_int (x mod 3)) (bool_of_int (x mod 5)))) then x + somme (x-1) else somme (x-1) ;;

let somme x = if x = 0 then x else somme2 x ;;
Run Code Online (Sandbox Code Playgroud)

同样的错误.

Gyo*_*yom 8

1)你的背诵永远不会停止if x == 0 then 0 else ...在开始时添加一个测试

2)你没有把括号括在你的周围x-1,所以ocaml读(somme x)-1.somme (x-1)改为写.