user12184817
user12184817

Reputation:

Why do I need a double semicolon in some statements outside the toplevel

I have the following two lines of code:

let a = [1;2;3;4;5;6;7;8;9;0]
print_string (String.concat " " (List.map string_of_int a))

It gives me this error:

File "test.ml", line 65, characters 0-0: Error: Syntax error

However, if I end the line with a double semicolon:

let a = [1;2;3;4;5;6;7;8;9;0];;
print_string (String.concat " " (List.map string_of_int a))

it works fine.

I've read, double semicolon should only be used in the top level, so why do I need it here. If I don't, what should I have written instead?

Upvotes: 1

Views: 401

Answers (1)

octachron
octachron

Reputation: 18902

Double semicolons can be used to introduce a toplevel expression:

let x = 0
let do_something () = Printf.printf "x=%d" x
;; do_something ()
;; do_something ()

Here without the separating ;; the first do_something () would still be part of the definition of the function `do_something.

It is generally considered more idiomatic to use an unit toplevel binding:

let x = 0
let do_something () = Printf.printf "x=%d" x
let () =
  do_something (); do_something ()

Upvotes: 4

Related Questions