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.
Related
I wrote a simple recursive fibonacci program that works fine without an assert statement, but when I add an assert statement, even with various permutations of parentheses, spaces, and double semicolons, I keep getting a syntax error during compilation.
Working function:
let rec fib n =
if n = 1
then 1
else
n*(fib (n-1))
Not working:
let rec fib n =
assert (n>=0)
if n = 1
then 1
else
n*(fib (n-1))
Any thoughts appreciated.
Thanks.
You have two expressions: assert (n >= 0) and if ... then ... else .... If you want the two expressions to be evaluated in sequence (which you do), you need to separate them with a semicolon:
let rec fib n =
assert (n >= 0);
if n = 1 then 1
else n * fib (n - 1);;
val fib : int -> int = <fun>
# fib 3;;
- : int = 6
# fib (-3);;
Exception: Assert_failure ("//toplevel//", 2, 4).
Extra spaces do not affect the semantics of OCaml programs. There are also no statements per se in OCaml - everything is an expression. To evaluate two expressions in a sequence you may use the semicolon, e.g.,
print_endline "Hello";
assert (1 > 2);
print_endline "World";
You can also use let .. in .. to chain expressions, especially, if you need to the expression values, e.g.,
let x = 1 + 2 in
let y = 3 + 4 in
Format.printf "%d + %d = %d\n" x y (x + y)
Going back to your example, it should be
let rec fib n =
assert (n >= 0);
if n = 1 then 1
else n * fib (n - 1)
P.S. The double semicolons are not really a part of the language but a special input sequence to be used in the interactive toplevel.
I'm writing a function, that takes date in (d, m, y) format. I need to count rez value, all of these +getNthInt function calls are adding certain elements from the list.
fun firstNewMoonInt ((d, m, y) : int * int * int) : int option =
let
if m = 1 orelse m = 2 then y - 1
else y
val rez = newStyleCorrection (d, m, y) * 100000
+ getNthInt(thousandCorrection, y div 1000)
+ getNthInt (hundredCorrection, y div 100 mod 10)
+ getNthInt (decadeCorrection, y mod 100 div 10)
+ getNthInt (yearCorrection, y mod 1000)
+ getNthInt (monthCorrection, m - 1)
+ getNthInt (calendarCorrection, y mod 4)
rez - lastSmaller(rez - 100000, reductions)
in
if rez div 100000 <= 30 then SOME rez
else NONE
end
I'm getting two syntax errors:
2.3-2.6 Error: syntax error: replacing LET with RAISE
13.3 Error: syntax error: inserting LET
Since I use all keywords for the constructions: let-in-end, if-then-else. I don't understand, what is wrong with my code?
Immediately inside a let, there should be a sequence of declarations. (Declarations are things like val x = ... or fun f x = ...). But in your code, there is an if which begins an expression.
You could fix this by making a new variable which is the result of the if expression:
let
val new_y =
if m = 1 orelse m = 2 then y - 1
else y
val rez = ...
And then you will need to figure out where to use new_y in the rest of the code.
Note that there is a similar problem just a few lines further down:
rez - lastSmaller(rez - 100000, reductions)
This is an expression where there should be another declaration. You could also fix it the same way: val new_rez = rez - lastSmaller (...) and then use new_rez where appropriate below that.
The compiler returns a Syntax error when using the command in
xyz aux
if((match4242 aux) = 0) then main (!list) else 1
Here's my full code.
open Printf
open Format
let regraUm m = m/2
let regraDois m = ((m / 10) mod 10) * (m mod 10)
let regraTres m = 42
let match4242 list =
let a = ref 0 in
let rec match42 list =
match list with
|[]->[]
|m::body->
begin
if (m = 42) then a := 1;
match42 body
end
in match42 list;
!a
let rec main aux =
let list = ref [] in
let rec xyz aux =
let () = List.iter (fun x -> printf "%d " x) aux in
match aux with
|[]->[]
|m::body ->
begin
if ((m mod 2) = 0) then list := (m - (regraUm m))::!list;
if ((m mod 3) = 0) || ((m mod 4) = 0) then
if (regraDois m <> 0) then
list := (m - (regraDois m)) ::!list;
if ((m mod 5) = 0) then list := (m - (regraTres m))::!list;
xyz body
end
in xyz aux
if((match4242 aux) = 0) then main (!list) else 1
The program checks if 42 is inside the list, if not then it calls itself again following a set of rules of division, subtraction, etc.
I don't know if this last info is helpful to debug this piece of code.
These two lines:
xyz aux
if ((match4242 aux) = 0) then main (!list) else 1
Represent a single expression, since they aren't separated by ;. But indeed you can't have an if expression in this position (function argument) unless you parenthesize it.
Most likely you want ; after aux here.
I am trying to make a program to compute the length of the Collatz sequence on all numbers from 1 to 100. Basically if I have an odd number I have to multiply it by 3 and add 1(n*3+1), and if I have a even number I need to divide it by 2(n/2) and then keep doing it untill it gets to 1 and in the end print out the count of times the number was either divided by 2 or multiplyed by 3 and added 1.
Here is what i have so far:
let stevec = ref 0;
let n = ref 1;
for i = 1 to 100 do
n := i;
while !n != 1 do
if (n mod 2 = 0) then
stevec := !stevec + 1;
n := !n / 2;
if (n mod 2 = 1) then
stevec := !stevec + 1;
n := 3 * !n + 1;
done
print_int (stevec);
done;;
After I run the code I get a syntax error and the print_int get underlined so i guess there is a problem with that but I'm not even sure about that.
There are several problems with your code, so let's take a look at it.
let stevec = ref 0;
let n = ref 1;
You shouldn't write that kind of code, as ; is an expression separator (and you are using it here as a declaration separator).
The right approach depends on wether you want your declaration to be local or toplevel.
(* local declaration *)
let stevec = ref 0 in
let n = ref 1 in
(* toplevel declaration *)
let stevec = ref 0;;
let n = ref 1;;
Then you typed while !n != 1 do. This shouldn't be used as you do physical inequality between your integers, whereas you want structural equality. Well, it will work too because of OCaml's behavior on integers but good practice requires you to use <> instead of !=.
Now let's look at your loop's body:
if (!n mod 2 = 0) then
stevec := !stevec + 1;
n := !n / 2;
if (!n mod 2 = 1) then
stevec := !stevec + 1;
n := 3 * !n + 1;
Notice the absence of any fi or closing bracket? That's because in OCaml, only the next expression after the then is executed. And precedence over ; doesn't go as you want it to. You can use parens or the more explicit begin ... end construction. To prove that the begin ... end works, I replaced your second test by a else statement.
if (!n mod 2 = 0) then
begin
stevec := !stevec + 1;
n := !n / 2;
end
else
begin
stevec := !stevec + 1;
n := 3 * !n + 1;
end
Finally while ... done being itself an expression, you should put a ; at the end of it.
And that's how you remove the errors from your code.
Yet...
This is clearly not the "right way" to do it in OCaml. The main perk of FP is its closeness to maths and you are here trying to define a mathematical function. So let's do this in a functionnal way:
let is_even x = (x mod 2) = 0;;
let rec collatz counter n =
if n = 1
then counter
else collatz (counter+1) (if is_even n then n/2 else 3*n+1);;
let () =
for i = 1 to 100 do
print_int (collatz 0 i);
print_newline ();
done;;
Doesn't that look nicer? Feel free to ask for any clarification of course.
I'm a complete newbie in OCaml and trying to create a simple console program.
(*let k = read_int()
let l = read_int()
let m = read_int()
let n = read_int()
let d = read_int()*)
let k = 5
let l = 2
let m = 3
let n = 4
let d = 42
let rec total: int -> int -> int = fun i acc ->
if i > d then
acc
else
if (i mod k) == 0 || (i mod l) == 0 || (i mod m) == 0 || (i mod n) == 0 then
total (i + 1) (acc + 1)
else
total (i + 1) acc;
print_int (total 1 0)
But if I try to compile it, it fails:
PS C:\Users\user> ocamlc -g .\a148.ml
File ".\a148.ml", line 14, characters 2-180:
Warning S: this expression should have type unit.
File ".\a148.ml", line 22, characters 0-21:
Error: This expression has type unit but is here used with type int
So, looks like if expression cannot return value here (why?). I've added let binding
let k = 5
let l = 2
let m = 3
let n = 4
let d = 42
let rec total: int -> int -> int = fun i acc ->
let x' = if i > d then
acc
else
if (i mod k) == 0 || (i mod l) == 0 || (i mod m) == 0 || (i mod n) == 0 then
total (i + 1) (acc + 1)
else
total (i + 1) acc;
x'
print_int (total 1 0)
and it works, but raises another error:
File ".\a148.ml", line 23, characters 0-0:
Error: Syntax error
Line 23 is the next to print_int statement and empty, so it seems like compiler wants something else from me, but I don't know what.
UPD: ok, the working code:
let k = 5 in
let l = 2 in
let m = 3 in
let n = 4 in
let d = 42 in
let rec total i acc =
if i > d then
acc
else
if (i mod k) == 0 || (i mod l) == 0 || (i mod m) == 0 || (i mod n) == 0 then
total (i + 1) (acc + 1)
else
total (i + 1) acc
in let x = total 1 0 in
print_int x;
The problem is the misuse of semicolon (;).
Semicolon intends to be the sequence composition of two expressions. S1 ; S2 means that the compiler expects S1 to be unit type, computes S1 and S2 in that order and returns the result of S2.
Here you mistakenly use ;, so OCaml expects the second if...then...else to return unit and wants you to provide one more expression. Removing ; and adding necessary in(s) should make the function compile:
let k = 5 in
let l = 2 in
let m = 3 in
let n = 4 in
let d = 42 in
let rec total: int -> int -> int = fun i acc ->
if i > d then
acc
else
if (i mod k) == 0 || (i mod l) == 0 || (i mod m) == 0 || (i mod n) == 0 then
total (i + 1) (acc + 1)
else
total (i + 1) acc
Regarding your second function, you should replace ; by in to indicate that x' is used to compute the return value.
BTW, your total function looks weird since you use lambda expression in the function body. Explicit declaration is more readable:
let rec total i acc =
if i > d then
acc
else if i mod k = 0 || i mod l = 0 || i mod m = 0 || i mod n = 0 then
total (i + 1) (acc + 1)
else
total (i + 1) acc
I have also changed reference equality (==) to structural equality (=) though there is no difference among them in integer.