Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OCaml "else" Syntax error

I'm learning OCaml for the first time, and I am having a bit of trouble with an extraordinarily vague "Syntax error". When defining the function generateboxes like so:

let rec generateboxes a b = 
    if a = (add1 b) then (force_newline ()); (print_sting "Done!")
    else if [1] = (Array.get finalarray a) then (populatebox
    (numbertoposition a) a); (generateboxes (add1 a) b)
    else (generateboxes (add1 a) b);;

The complier gives the error message: "Syntax error" and it points to the first else. Is there anything glaringly wrong with my code for it to output such a message? (I realize the code is out of context, but if its a syntax error then it shouldn't matter).

like image 694
Balthasar Avatar asked Nov 22 '25 07:11

Balthasar


1 Answers

let rec generateboxes a b = 
    if a = add1 b then (force_newline (); print_sting "Done!")
    else if [1] = Array.get finalarray a then
      (populatebox (numbertoposition a) a; generateboxes (add1 a) b)
    else generateboxes (add1 a) b;;

If you have more than one statement in a then or an else clause, you need to put them inside parentheses. Alternatively, you can put begin ... end around them:

let rec generateboxes a b = 
    if a = add1 b
    then begin
      force_newline ();
      print_sting "Done!" end
    else if [1] = Array.get finalarray a
    then begin
      populatebox (numbertoposition a) a;
      generateboxes (add1 a) b end
    else generateboxes (add1 a) b;;

(Note that I have also removed a few unnecessary parentheses to make the code clearer.)

like image 128
jrouquie Avatar answered Nov 24 '25 22:11

jrouquie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!