2
votes

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).

1
I recommend you put no more than one "thing" per line and use a text editor that performs automatic indentation. Emacs with caml-mode, tuareg-mode or typerex is great for that. All you have to do is press the tab key once to indent the current line properly. That would have explained your syntax error right away. - Martin Jambon

1 Answers

8
votes
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.)