Reputation: 151
I am writing a function in OCaml to raise x to the power of y. My code is:
#let rec pow x y =
if y == 0 then 1 else
if (y mod 2 = 0) then pow x y/2 * pow x y/2 else
x * pow x y/2 * pow x y/2;;
When I try to execute it, I get an error for syntax in line one, but it doesn't tell me what it is.
Upvotes: 0
Views: 117
Reputation: 1329
When you wrote the code, did you type the #
? The #
is just a character that the OCaml REPL outputs to prompt for input; it is not part of the code. You should not type it.
Here are some other errors that you should fix:
==
is physical equality in OCaml. =
is structural equality. Although both work the same for unboxed types (such as int
), it's better practice to do y = 0
. Note that you use =
, the recommended equality, in the expression y mod 2 = 0
.y/2
. pow x y/2
parses as (pow x y) / 2
, but you want pow x (y / 2)
.Upvotes: 1