I wasn't able to find a clear answer on Google, but it seems multiline if-statements are discouraged in OCaml (?) The ones I see with multiple lines seem to have begin end
keywords in them.
I'm currently getting this error on the line num = (num - temp) / 10
, characters 25-27 : Error: Parse error: "end" expected after [sequence] (in [expr])
. If I remove all of the begin end
then I get the error Error: This expression has type bool but an expression was expected of type int
at the same line.
let rec reverse_int num =
if num / 10 == 0 then begin
num
end else begin
let temp = num mod 10 in
num = (num - temp) / 10
let numDigits = string_of_int num
temp * (10 * String.length(numDigits)) + reverse_int num
end;;
You probably mean something like the following.
let rec reverse_int num =
if num / 10 == 0 then begin
num
end else begin
let temp = num mod 10 in
let num = (num - temp) / 10 in
let numDigits = string_of_int num in
temp * (10 * String.length(numDigits)) + reverse_int num
end;;
Problems here:
line num = (num - temp) / 10
is a value of type boolean. What you mean is that you want, in what follows, num
to have the new value (num - temp) / 10
and continue evaluation; hence replace this line with let num = (num - temp) / 10 in
.
lines
let numDigits = string_of_int num
temp * (10 * String.length(numDigits)) + reverse_int num
are parsed let numDigits = string_of_int num temp *...
which yields a type error, as the function string_of_int
only has one argument. Here the in
is necessary.