Learning OCAML

I decided to increase my perspective in programming and this time to look at some functional language.  Functional languages are here for quite some time,  but never got into mainstream, however recently they are getting more popular.   And some features from functional programming made their way into other languages (first class functions, closures, higher order functions, … ), so I was curious  to learn a bit more of functional programming. For my exercise I decided to try OCAML.


OCAML might look as bit obscure language at first look –   (in TIOBE index on 50th place), but I’ve learned few nice things, about it, which convinced me to give it a try:

  • Works well in Linux (which is my primary platform) and  compiles to native code, but it is multi-platform  (compiles on Mac, Win …). Thanks to compilation to native code OCAML programs can provide good performance.
  • OCAML is a  base for F# – in fact, with some limitation F# should be able to compile OCAML source.   F# is becoming more and more popular,  so I guess knowledge of ML family language could be useful.
  • It’s said that OCAML has all that is considered essential for true functional programming,  but also has provision for imperative and OO programming, which can be used where it’s necessary for program efficiency.
  • OCAML is learned as universities at advanced programming courses about functional programming.   (for instance at Cornell – this course and this course)
  • OCAML was criticized for rather limited standard library and lack of libraries for many common tasks,  but this has changed with availability  OPAM – an excellent package manager for OCAML. Many packages are available there including JaneStreet Core – extended and improved alternative to OCAML standard library.

Learning Resources

Many resources are available online – many for free – you can check them  here or here or here.

I’ve chosen to use Real World OCaml book, which is practical book using Janestreet Core library from beginning and is available online for free.  The only thing I was missing there were exercises, because the only way how to learn programming language is to write code and before one is ready to jump into some bigger project some well designed exercises are invaluable.   So I complement reading  this book with exercises from Cornell’s university “Data Structures and Functional Programming”  (I’ve chosen older semester, but also newer semesters are are available).

Setting Environment

Basically I followed this instructions (from the book site),  just be sure to follow all details, especially:

  • Add opam configuration to .bashrc  (this can be chosed during opam init) and restart shell
  • Set ~/.ocamlinit  as described there

As Editor I do prefer Eclipse, so there are basically two options:

ODT – OCaml Development Tools – this Eclipse plugin is recommended in the instructions
OcaIDE – this is alternative plugin,   seems to have more features then ODT,  this one I’ve chosen for now. But there are some challenges to make it work with Core and OPAM packages.

In order for OcaIDE to work, Eclipse must be started from environment , where some variables are set –   I use this script to start eclipse:

In Eclipse project create project OCAML Project (ocamlbuild),   in project properties set Project/Other Flags:

This will basically build code same way as corebuild script.   For any additional libraries you need add -pkg option.

However I still have problems with OcaIDE –  code completion and  functions help are working only for OCaml standard library, I did not find way how to make them work for Core.Std or for any additional library.   In debugging Variables view is not working if additional libraries are involved.    If anybody has experiences with OcaIDE and know how to get additional/alternative libraries here in play, please let me know.

One thought on “Learning OCAML”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">