ocaml

Compile a one file ocaml program with a module and a main method


I somehow can't install a working OCaml environment on my Windows computer. I want to use know the OCaml online compiler. Problem: I can only write one file there and put everything in that one file:

module Atom = struct

  let hello name = print_string ( "Hello " ^ name );;

end

print_string "Hello, World!";
Atom.hello "Florian";

The above code produces the error:

Output:
File "./HelloWorld.ml", line 7, characters 0-12:
7 | print_string "Hello, World!";
    ^^^^^^^^^^^^
Error: Syntax error

Without the module Atom stuff, the print_string works! Is it not possible to define a module at the top of the file. Or where is the syntax error?


Solution

  • You are missing a ;; at the end of the module declaration:

    module Atom = struct
      let hello name = print_string ( "Hello " ^ name )
    end;;
    
    print_string "Hello, World!";
    Atom.hello "Florian"
    

    Related documentation.