3

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;;
stumped
  • 3,235
  • 7
  • 43
  • 76
  • 2
    (1) the recursive call uses `rev_int`, should be `reverse_int`, (2) have you tried adding `let...in` to `num = (num - temp) / 10` and an `in` after `let numDigits = string_of_int num`? I added those and it compiles. – Hunan Rostomyan Aug 18 '16 at 21:41
  • Sorry, in my code it's all rev_int. I just changed it in StackOverflow to show clearer what the method does, but I forgot to change the second one in this post. – stumped Aug 18 '16 at 22:07
  • Thank you! Compiles now, I was also wondering why the `let...in` for `num =` is necessary? – stumped Aug 18 '16 at 22:12
  • You're trying to declare a local variable to hold some temporary value. It will hold this value not globally, but in an expression that follows, so you specify the expression by saying (let identifier `num` stand for the following quotient in the expression that follows `in`). More experienced folks will give a more satisfying answer. That's my guess; could be wrong. – Hunan Rostomyan Aug 18 '16 at 22:28
  • I'm confused on why t it's not using `num` from the function's argument – stumped Aug 18 '16 at 22:29
  • 1
    Variables in ocaml are *immutable*, so you can't just assign to `num`. You can temporarily shadow it by a nested `let` as you now have, but to modify a variable you should look into references/assignables. – Hunan Rostomyan Aug 18 '16 at 22:34

1 Answers1

2

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.

mookid
  • 1,132
  • 11
  • 19