My first blog post on this website was about Haskell. I've always been interested in functional programming but the complexity of Haskell never seemed worth.
I have been vaguely aware of OCaml for a long time but never dipped my toes in. It has been having a bit of a renaissance recently with multicore support, effects and a more systems focussed spin called OxCaml from Jane Street. Now seems a great time to learn it and I was curious about Eio, a concurrency framework. It uses the aforementioned effects but more interesting to me is it is meant to be deterministic, which could be helpful for testing.
Books & Tutorials
The first thing to do is pick a tutorial or a book. A quick google search later I had found Real World OCaml. Assuming that it would be similar to Real World Haskell (which I enjoyed and was very hands on) and assuming I already had sufficient FP chops, I dived in.
Impatient to look at syntax and semantics I skipped over the more prosey Prologue. If I had read it I would have been told that the book chooses to use Jane Street's Base standard library replacement. It seems that historically the real standard library was quite anemic, only intended to cover the needs of the compiler. This is no longer true and it has slowly been growing. Still, it doesn't seem unreasonable to me that a book called Real World OCaml use the libraries from the biggest real world user of the language.
Anyway, I surely learnt my lesson to read things more carefully and went back to the beginning to start again. The first half of the book is well written and I happily followed along. I started to get lost around GADTs and first class modules though. I think the book lacks for exercises, so once something unfamilar comes up you don't have the full grasp of the basics to bail you out.
I went searching for something a bit easier and found CS3110 which has both videos and text to suit all tastes. It also had exercises which helped a lot. I would suggest to people that they start with this course and then graduate onto RWO for the more advanced features. I like RWO although if you are looking for a project based book like RWH, you'll be disappointed.
How It Feels In The Hand
Overall OCaml is very enjoyable to write code in. It has a nice mix of functional and imperative programming. For example, to loop through a list you are either using a recursive function or List.map and friends:
let print_recursive list = let rec loop i list = match list with | [ ] -> ( ) | x :: xs -> Printf . printf "[ %d ] => %i " ; loop ( i + 1 ) xs in loop 0 list let print_fn = List . iteri ( fun i x -> Printf . printf "[ %d ] => %i
... continue reading