I ran into two related problems starting ocaml today. Maybe I’ve made some bizarre error, but I can’t see how. The first is this
let hello = “hello world” in print_endline hello
It doesn’t work. And according to lots of material around the internet, it should - it’s copied straight from Values, expressions, and bindings — OCaml From the Ground Up. The error message was generic…
File “two.ml”, line 22, characters 26-28:
22 | let hello = “hello world” in print_endline hello
^^
Error: Syntax error
But I took a guess that this would fix it -
let () = let hello = “hello world” in print_endline hello
And it did. Has there been a change to ocaml syntax in the past few years to explain why the first version fails??? If so, I would suggest warning people. And even making some effort to contact people to have the old version removed. And if there hasn’t been a change, what is happening and can someone point me to a clear explanation of the relevant rules???
(Added: but that code DOES work in the REPL. Which seems crazy - the point of a REPL is to be able to experiment and see what will work in compiled version, surely?)
The second problem is this…
let d =
let a = [ 1; 2; 3; 5; 7 ] in
let b = [ 1; 2; 4; 8; 16 ] in
let hello = “foo to you” in print_endline hello;
List.map2 ( * ) a b
That works. But this doesn’t
let hello = “foo to you” in print_endline hello;
…And the explanations I’ve seen of the use of the semicolon wouldn’t have lead me to predict this.
My best guess at the moment is that Unit return types could once be ignored but this is no longer the case and they have to be absorbed by a () or _. Unless you are in the body of an expression, in which case you can ignore them by using ending with a ;
..If so, this needs explaining clearly - and I would suggest early on, because it is going to come up as soon as people start writing code snippets to test their understanding, which is what any good programmer will do in the first few minutes of learning a language. Or if I’m wrong, then whatever IS right needs explaining. And what is happening with the REPL???