gpt4 book ai didi

recursion - OCaml 中的递归函数

转载 作者:行者123 更新时间:2023-12-03 06:59:49 25 4
gpt4 key购买 nike

我有一个小问题:我想解决this problem与 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?).

我做错了什么?

<小时/>

我尝试过的新代码:

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 ;;

同样的错误。

最佳答案

1) 你的回避永远不会停止,在开头添加一个类似 if x == 0 then 0 else ... 的测试

2) 您没有在 x-1 两边加上括号,因此 ocaml 会读取 (somme x)-1。改写 somme (x-1)

关于recursion - OCaml 中的递归函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1952695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com