My OCaml Journey — Starting Out

Chris Duncan
3 min readJul 6, 2021

--

Photo by Yeo Khee on Unsplash

Every so often I get the urge to learn a new programming language. When I say new, I mean new to me. I’ve had a look at functional languages in the past, including Clojure and Haskell, but I felt that OCaml warranted some scrutiny. What prompted me was an article that I read about the trading firm, Jane Street, who use OCaml extensively to enable their business. After all, if it’s good enough for a trading firm with plenty of money to be won and lost then surely it must have some compelling features.

I use the Ubuntu operating system; just the standard, latest stable desktop version. I also use Visual Studio Code (aka VS Code) as my editor for programming. I’ve used vim and tried Emacs, but VS Code suits me and I don’t care who knows about it.

Getting Tooled up

A little bit of googling later and I was on the Up and Running with OCaml page from the OCaml website which took me through the setup procedure. When I was finished I had OCaml installed along with opam, dune and utop, as well as the OCaml Platform extension for VS Code.

opam is the OCaml package manager, dune is the build system and utop is an improved REPL.

An annoying issue that I had was that every time that I started a new bash shell, I had to run the following command in order to set the correct environment variables to access the utilities that I had installed via opam.

eval `opam env`

However, my problem was solved after a bit more googling where I found the following handy snippet which I put in my .bashrc file to set up the shell environment automatically.

. ~/.opam/opam-init/init.sh > /dev/null 2> /dev/null || true

So far so good. I had what I needed in terms of tooling but not much of a clue about coding in OCaml. Next stop, some learning resources. Here are some resources that I found and continue to find helpful -

Editors

I tried Emacs again. Maybe the Emacs OCaml combo was the way to go. For me at least, it wasn’t. Try as I might, I couldn’t get the OCaml LSP Server to work, which is probably more due to my lack of Emacs-fu than anything else. Emacs OCaml IDE functions can also be implemented with tuareg and merlin, but I found these packages offered a less pleasant experience compared to my VS Code setup.

After my Emacs struggles I had zero motivation left to try to explore any vim OCaml options. Maybe I’ll investigate in the future.

The VS Code OCaml Platform extension provides good code-completion and lookup functionality and I’ve only scratched the surface of its capabilities.

Impressions so far

I like OCaml. I already knew that I was a fan of immutability, pattern matching and other functional goodness from my Clojure/Haskell hacking experience. However, while OCaml provides more than enough for the functional enthusiast, it also provides for those who want to program in object-oriented and/or imperative styles. The fact that it has static typing and excellent type inference are additional big plus points.

A surprising thing was that OCaml has a number of standard library candidates. The ones that I know about so far are -

After some more googling I decided to go for Base as my preferred stdlib. This might prove to be a mistake but I’m happy with that choice for now.

One thing that has been an obstacle to my learning is a shortage of examples when I’m trying to figure out how to use particular module functions. Yes, the documentation provides function type signatures and I have been able to figure out the correct syntax most of the time but this is not ideal. Sometimes, I have to spend quite a bit of time in a trial and error fashion to get to what I need and I can’t help feeling that added examples in the documentation would be very helpful for a beginner.

I’ll post further about my progress with OCaml from time to time. If anyone has useful hints and tips concerning learning OCaml then I’d be glad to hear them.

--

--

Chris Duncan
Chris Duncan

Written by Chris Duncan

0 Followers

IT industry veteran, lifelong learner and lover of simplicity over complexity

No responses yet