Recitation 1: Introduction to OCaml Syntax
We will use the Objective Caml (OCaml) programming language this semester. OCaml is a functional
language rather than a procedural language; the key difference between these two classes of languages is the
---the way in which programs are executed.
) languages, such as C
and Java, are based on commands that change the state of the machine on which they execute. For example,
the assignment statement (in both C and Java) explicitly changes the value stored in some memory location.
languages, in contrast, are based on evaluating expressions to produce values. OCaml provides
good ways to build correct, understandable, and large expressions. The focus of this lecture is understanding
For real applications, programming with expressions may seem like a stretch. After all, applications often do
not "compute" anything; instead, they allow you to "do" something. Later in the course, we will introduce the
, and do
while you're at it---e.g., evaluate an expression, and (as a
side-effect) display a window on the screen. But for the moment, we will live in a
world, one without
To start learning OCaml, you should begin playing with the OCaml language by writing toy expressions.
Like most programming languages, Ocaml has a compiler that can be run on source files to produce object
files. But to really understand expressions, there is a better way to interact with the OCaml compiler, called
. This system is something like a very fancy calculator---you interact with it by
typing in an expression, and it responds by evaluating that expression and telling you the resulting value.
: In previous semesters, this course has been taught in SML, a language very similar
to OCaml. They share both a common origin and almost all of their syntax, but OCaml is currently better
supported than SML, and looks to have better support in the future as well. OCaml also has better secondary
features, such as debugging. We will try to update recitation and lecture notes to use the correct OCaml
syntax as we cover material, but we may miss things.
If we do miss things, don't panic. For aid in understanding notes from previous years or notes that may not
have been updated, you may wish to consult the
SML vs. OCaml
page, which provides a detailed list of the
differences between the two. Again, the two languages are very similar: for most of the standard syntax,
almost everything is the same except the keywords. With a good understanding of OCaml, it is easy to
Starting the OCaml toplevel system.
You can find current releases of OCaml at
the OCaml home page
Once you have OCaml properly installed, you will be able to start an interactive session by typing