I have a little problem: I want to solve this problem with OCaml, so I tried this ->
-> 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?).
What have I done wrong ?
New code I tried:
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 ;;
Same error.
1) your recusion never stops add a test like if x == 0 then 0 else ...
at the beginning
2) you don't put parentheses around your x-1
, so ocaml reads (somme x)-1
. Write somme (x-1)
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With