This is a report of the first steps on my journey to learning OCaml.
To give you some context, I am a relatively experienced programmer with very little free time, so I was looking for a crash-course, i.e. the quickest possible introduction to the language, so that I can get to work and learn as I implement my side-project.
I know there’s work being done on the documentation site, and that’s fantastic. This report dates back to a month ago, so it might be that some resources have been updated in the meantime. cc @professor.rose who asked me about my experience as a newcomer
Anyway, here goes:
- The first question is: which resource do I use? The choice seems to be between Learn OCaml and RWO. I ended up choosing the former
- Digression: I’m a Nix user, so had to first figure out how to take procedural install instructions (e.g.
opam switch
) and turn them into declarative environment declarations for a Nix shell - Setting up LSP: First I needed to figure out the difference between OCaml-LSP and Merlin (turns out, the former uses the latter);
- the official docs say that Vim users don’t need to setup up LSP, and use Merlin directly – why?
- how do I test for equality? No mention of the semantics of
=
that I could easily find- even “worse”, no mention of the highly unusual
<>
- even “worse”, no mention of the highly unusual
- I ended up finding this info here, but knowing what to search for, the Cornell course also has this info
- the stdlib pages don’t list individual functions in the sidebar, which makes it hard to skim them for what I’m looking for
- additionally, they don’t provide any examples, which makes it much harder to figure out how to use the functions
;
vs;;
vsin
: here I got confused. The examples only deal with the top-level, and introduce the;;
syntax which AFAIU is only used there. I got tripped up by the difference between;
andin
.
For example, running ocamlc
against this is a syntax error:
type nucleotide = A | C | G | T
let hamming_distance s t =
(* iterate over s and t, whihc have the same length, using an index i.
compare the character s[i] and t[i]. if they're different, increase the number
*)
let distance = ref 0 in
let func i c =
if s.[i] <> t.[i] then
(* distance := !distance + 1 in *)
incr distance;
String.iteri func s;
distance
(it needs to be String.iteri func s in
)
But this works:
type nucleotide = A | C | G | T
let hamming_distance (s: nucleotide list) (t: nucleotide list) =
(* iterate over s and t, whihc have the same length, using an index i.
compare the character s[i] and t[i]. if they're different, increase the number
*)
let distance = ref 0 in
List.iter2 (fun x y -> if x <> y then incr distance) s t;
distance
There is obviously a good reason for this, but it would be great to have a place I can easily find which explains this. I couldn’t find one.
- compiler errors: compiling the failing snippet above only returns:
$ ocamlc hamming.ml
File "hamming.ml", line 16, characters 0-0:
Error: Syntax error
It would be nice to have the compiler spit out the line where the syntax error happened, and maybe even tell me what I should do instead (in this case, ;
should become in
)