I am quite new to OCaml, so apologies if this is a silly question. I have this OCaml file:
type tree =
| Node of int * tree * tree
| Leaf of int
let t = Node (3, Node (4, Leaf 1, Node (3, Leaf 5, Leaf 2)), Leaf 1)
let rec height t =
match t with
| Node (n, t1, t2) -> 1 + max (height t1) (height t2)
| Leaf n -> 0
let _ = print_string (string_of_int (height t))
If I compile the file with ocamlc -o out my_file.ml, it compiles and runs as expected. However, if I try to run the file with ocaml < my_file.ml, I get a syntax error in the definition of my height function.
Placing a double semi colon after my height function as such:
let rec height t =
match t with
| Node (n, t1, t2) -> 1 + max (height t1) (height t2)
| Leaf n -> 0
;;
Fixes the issue.
My questions are:
- Does OCaml require the double semi colons after a pattern-match only in interactive mode? That seems to be where the issue is coming from.
- Does
ocamlformatknow this? When I runocamlformaton the file, it automatically places a double semi colon after myheightfunction. I was confused as to why it was doing that until I got the syntax error.
Thanks in advance for the info! I've looked for answers to these questions but have been unsuccessful. I did find this blog which claimed the double semi colon is never necessary.