Same code doesnt work with ocamlc vs online editor

type 'a stack = 'a list ref

let create () = ref []

let add stack x = stack := x::!stack

let pop stack =
  match !stack with
    [] -> raise (Invalid_argument "pop")
   |x::xs -> stack := xs;
             x

let size_of stack =
  List.length !stack

let my_stack = create ()

let _  = add my_stack 3;

let popped = pop my_stack in Printf.printf "%d\n" popped
 

When I try to compile this

 ocamlc Stack.ml -o Stack

File "Stack.ml", line 20, characters 26-28:

20 | let popped = pop my_stack in Printf.printf "%d\n" popped;
                                                  ^^
Error: Syntax error

This same code works in TryOcaml online editor.

Thanks in advance!

The trailing ; is the source of the syntax error, it makes the rest of your code reads as

let _  =
  add my_stack 3;
  let popped = pop my_stack in Printf.printf "%d\n" popped

The playground toplevel is then discarding this last incomplete sentence because you may complete it later whereas ocamlc has to fail on the incomplete sentence.

3 Likes