Got this code :
let abs x =
if x<0 then begin let y=x*(-1);print_int y;end;
else print_int x;;
It gives me syntax error on the "end", what am I doing wrong ?
Stop using begin and end. IIRC parentheses work too.
>>61140968
I am very new in coding, what are thoses parentheses ?
Btw I would appreciate to have a mentor, who will be able to tell me where the error is, so I dont have ton ask on a forum each time
>>61140871
been a while if I've programmed in caml, but you probably want to do something like thislet abs x =
let y = if x < 0 then -x else x in
print_int y
or even thislet abs x =
print_int (if x < 0 then -x else x)
Ok, something else, why EACH TIME I put 2 let in my code, it goes wrong ?
let cpt x y =
(let z=x);
while !z<>y do
print_int (!z);
z:=z+1;
done;;
I'm trying to print all numbers from x to y, but if I want to incr x, I need to put it in a variable (tell me if there is a way for directly edit x, but I dont think so, its just an int, no ?). So I put a "let z=x;" so now I can edit z, which is just like x but as variable. Now the code dont works anymore, it show a syntax error on "done;;". And this happen each time I put 2 let in the same code, why ?
Edited code but still the same problem :
let cpt x y =
let z=ref x;
while !z<>y do
print_int (!z);
z:=z+1;
done;;
>>61143327
For local constants, you must uselet z = x in while ....