Ocaml track: introduction


This is the home page for the Ocaml (Objective CAML) track of CS 11. This information is for the Spring 2015 term.


Overview of the track

This track has been created in response to a growing demand from students for a track which teaches the Objective CAML (Ocaml) programming language, and also teaches functional programming, which is one of the programming paradigms that is supported directly by the Ocaml language.


Administrative information

All the administrative information for this track is on this page.


About the Ocaml programming language

The full name for Ocaml is Objective CAML. The "objective" part means that it's an object-oriented extension to the CAML language. "CAML" is short for "Categorial Abstract Machine Language", and we won't discuss what that means here ;-) Ocaml is a computer language in the ML family (another language in the same family is Standard ML). Some of the many features of Ocaml that make it different from other computer languages include:

Ocaml is not a perfect language; some of the syntax is pretty nasty and confusing, error messages can be somewhat uninformative at times, the learning curve is steep, and there are times when the type system is too rigid for some applications. Despite this, most experienced programmers really enjoy using Ocaml, and many of us (including me) consider it among our favorite languages. It is definitely a very rewarding language to learn and to use.


About functional programming

Functional programming is a paradigm of programming (i.e. a particular way of writing programs) which contrasts greatly with the more common imperative and object-oriented paradigms. Ocaml supports all three paradigms, but it's most often used as a functional programming language (or as a mostly-functional programming language), and that how we will use it for most of this track. But what exactly is functional programming?

First off, functional programming means that functions are data. This means that functions can be passed as arguments to other functions, they can be created on-the-fly, they can be returned from a function, and they can be stored into data structures. Most computer languages (e.g. C) will allow you to pass a function as an argument to another function (although Java doesn't even allow that) but won't allow you to create a function on the fly. In addition to this, functions can hold references to data objects that existed when they were created and use them when they're invoked (such functions are called "closures" and are really like lightweight objects).

Another key aspect of functional programming is that data should be persistent. Put differently, it means that you avoid using mutable data as much as possible. So re-assigning the values of variables is strongly discouraged in functional programming. Most programmers probably think that it's impossible to program without using mutable variables all over the place, but it is indeed possible; it just requires you to think differently. Instead of using variables, you use constants: values that are bound to an identifier which don't change after being assigned. This is more like the way we think in mathematics: when we say that "a = 5" we mean that "a" represents the number "5" from now on, not just until we change it to something else. In fact, one of the interesting aspects of functional programming is that it's much more amenable to mathematical verification of algorithms than imperative programming is.

One result of the requirement that data should be persistent is that assignment statements are rarely used in functional programming.

Another result of the requirement that data be persistent is that looping statements of the kind seen in C, C++ and Java (for loops and while loops) are rarely used; instead, we use recursion to do computations that involve loops.

Yet another result of the requirement that data be persistent is that we almost never use arrays for storing aggregate data; instead we use singly-linked lists. That's because singly-linked lists are persistent: you can add an element to the front of a list, and a variable which referred to the original list will still refer to the same data. Functional programmers tend to use lists a lot, and tend to use different data structures than imperative programmers.

Having said all that, sometimes imperative programming (with mutable data structures, assignments, arrays, etc.) is just the most efficient way to get a job done, so Ocaml also fully supports imperative programming. In general, though, you should strive to write your Ocaml programs in a functional way if possible, and only use the imperative features if you really need to (e.g. for a major boost in efficiency). Functional programs are much better-behaved than imperative ones, and they are usually much easier to debug.


Lectures


Assignments


Grades

Current grades for all students are located on this page.


Books and References