Getting Started with OCaml

OCaml, also known as Objective Caml, is a general-purpose, statically typed functional programming language. It is the main dialect of the Caml Programing language, extending the core language with an object-oriented layer. OCaml runs on Linux, Mac OS X, Windows, and FreeBSD operating systems.

OCaml is commonly used in education, on top of commercial uses like enterprise programming, scientific research, and financial computing.

What is OCaml?

OCaml is a multi-paradigm (imperative, functional, object-oriented) programming language derived from the Caml programming language.

The language is very efficient — useful in quickly developing and deploying applications. OCaml's rich static type system resembles a set of analysis tools that improve the quality of the code. The original Caml programming language was derived from the ML programming language. And many modern programming languages like F# and Scala have been influenced by OCaml to some degree.

OCaml is a free, open-source project maintained by the French Institute for Research in Computer Science and Automation (INRIA), published under the Q Public License (QPL). The OCaml online community is active on GitHub, Bitbucket, StackOverflow and various mailing lists.

Brief History Of OCaml

Work on OCaml development was started by INRIA's Xavier Leroy and others in the early 1990s. Didier Remy and Jerome Vouillon designed a highly expressive type system for objects and classes. This design was first implemented in Caml Special Light, that eventually led to the creation of Objective Caml language, first released in 1996.

Objective Caml was the first programming language that combined the power of ML-style static typing and type inference with object oriented programming methods. In 2011, Objective Caml was renamed to OCaml.

The latest stable version of OCaml is 4.02.3. It was released in July 2015.

Features

OCaml is a functional, imperative, and object-oriented programming language with a static type system that can help eliminate runtime errors. OCaml's compiler with type inference does not require manual type declarations.

Programs written in OCaml start and run much faster, and usually consume far less memory, than a C# or Java program. OCaml accomplishes this thanks to type inference, a shorter syntax, and OCaml's extensibility.

OCaml's other features include parametric polymorphism, pattern matching, tail recursion, first class lexical closures, and exception handling. OCaml also provides a foreign function interface to support linking to C primitives.

OCaml also features a garbage collector for automatic memory management. Its garbage collector does not support concurrency, so symmetric multiprocessing is not supported. However, there are several libraries that bring support for distributed computing to OCaml.

Getting Started with OCaml

At first glance, getting started with OCaml can seem difficult, especially if you do not have a background in functional language programming. Also, effective use of the OCaml type system can prove tricky when you are making your first steps in OCaml programming. But the language is actually easier to learn than it first appears.

OCaml Development Environment

The first step in getting started with OCaml is setting up your development environment. OCaml is currently supported on pretty much any operating system you might be using.

The official OCaml download and detailed information dealing with the installation of its compiler and packages on each platform can be found at the OCaml.org install section. The installation and management of libraries is made easier with the package management tool OPAM.

You can also download a specific version of OCaml source code and compile it, if you choose to do so.

Resources

Since OCaml does not have the kind of mainstream appeal of a language like C++, your choice of resources is somewhat limited. We tried searching the web for the best OCaml resources, including ebooks, tutorials, and online courses. While we did manage to find a number of quality OCaml resources, it should be noted that your choice of OCaml learning aides will be limited.

Free Interactive Courses

We chose to single out just two OCaml interactive courses. Both provide a lot of useful information, along with step by step tutorials and some code examples:

  • Try OCaml is an interactive environment where you can explore OCaml in your web browser with an online compiler. It also contains an online step by step tutorial demonstrating the power of the language.
  • Functional Programming Course in OCaml is a complete online university course in OCaml with lectures and many useful links.

Additional resources

Working your way through courses and tutorials will be easier if you use the programming language reference and additional code samples. Some of these additional OCaml resources will help you try out basic skills and master new ones:

These resources should help you master the basics, and test your skills. Example code will be a vital resource for people with limited functional programming experience.

Free Ebooks

There aren't that many OCaml ebooks out there, but we managed to find a few, and decided to single out two of them:

  • Real World OCaml by Minsky, Madhavapeddy and Hickey takes you through the concepts of the OCaml language and helps you explore the tools and techniques that make OCaml an effective and practical tool.
  • Using, Understanding, and Unraveling The OCaml Language by Didier Remy is composed out of course notes. Many exercises with different levels of difficulty are also included.

Summary

OCaml is a great general purpose language for people who just want to get work done. But despite its power, it has a fairly simple syntax. With the resources presented here, you should be on your way.