OCaml Intro: You can have it all: Object-Oriented, Imperative, and Functional
YAPL
So why take the time to delve into yet another programming language? I find at worst, one can expands one’s knowledge of existing programming paradigms by studying the language design choices of other languages not used on a daily-basis. Best case, one discovers a new language that is rich in expression and productivity; thereby refining the productivity of the user.
OCaml
So, I tripped over this programming language in a rather random and circuitous path. I was watching a super-cool video visualizing how the Fourier Transform integral is constructed (by 3Blue1Brown). At the end of the video, the sponsor was a well-known, prestigious wall-street boutique, Jane Street, famous for hiring the best of the best in mathematics and computer science. So, naturally, I thought I’d take a closer peek at their website and discovered that they were big sponsors in OCaml as it underlies their trading and production environments. And down the rabbit hole I went.
Getting started.
My strategy for taking on a new language is one of breadth-first scan followed by depth-first curiosity analysis - delving into the minutia to understand subtle language features piece-wise. At the top-level, first things first, we need to understand how to install and interact with the language broadly speaking.
Installing
To that end, we need to install the environment. There’s great documentation describing how to install OCaml for both MacOSX and Linux. Be sure to install the base OCaml language, as well as the package manager, opam. This companion utility is essential for adding additional packages that extend the standard libraries.
REPL
After successfully installing the OCaml environment, let’s take the OCaml REPL for a spin. Be sure to setup the terminal environment variables. We can do this easily using the ocaml command itself. In your terminal, execute the following (or better yet add it to your shell init profile).
$ eval `opam config env`
And now that you’re all setup, you can start the REPL using OCaml.
#let greeting = "hello world!"
val greeting : string = "hello world!"
;;
The # is the expression prompt, and ;; is the terminator. Typing let e = e ;; is an expression assignment. And the response from the expression evaluation is the line below the expression - val greeting : string = “hello world!”.
Okay, if you got this far, you have a working installation and you’re reading to dive in further and look at language features. Here are few links to start with:
- OCaml Main website
- Documentation and User’s Manual
- 99 Problems (and solutions) in OCaml
- OPAM Packages
- Cheat Sheets
Next Steps
Now that you’re immersed into the OCaml community, the next blog post will demonstrate how to solve trivial problems in OCaml. Go read up on the language sytanx and structures, and stay tuned for the next post!.
-- rob