OCaml
Readings from the OCaml documentation:
- A First Hour with OCaml, up to and including “Our Own Data Types”.
- Data Types and Matching
OCaml stands for Objective Caml, because it extended Caml with object-oriented features (which we will not cover). Caml was called that because it was an ML that compiled to a VM called the Categorical Abstract Machine (which OCaml no longer uses). And ML stands for Meta Language, because it was originally designed as the metalanguage for an automatic theorem prover (which no longer exists). The ML family of languages also includes Standard ML, F#, and (more loosely) Scala and Haskell.
Running OCaml
OCaml is installed on studentnn.cse.nd.edu
, where
nn
∈ {04,05,06,10,11,12,13
}, at
~dchiang/Public/pl/bin/ocaml
. To install it on your own
computer, most package managers can install a package called
ocaml
that is just the compiler/interpreter, or for a more
complete installation, you can follow the instructions on the
OCaml website.
Now create a file called hello.ml
:
#!/usr/bin/env ocaml
print_string "Hello, world.\n"
To run it, make sure that ocaml
is in your
PATH
, and type
ocaml hello.ml
or make it executable and run it directly:
chmod +x hello.ml
./hello.ml
The OCaml language
Although ML has the lambda calculus at its core, just like Scheme, programming in ML has a very different feel from programming in Scheme. The syntax is a superficial difference that you’ll hopefully get accustomed to quickly. The type system is the real difference, and there are three main ideas about types that I’d like you to take away from the reading:
Type inference, which gives all the advantages of types (statically catching programmer errors) without one of its disadvantages (making the programmer write types explicitly).
Polymorphism, which is quite different from the polymorphism you may have learned about in object-oriented languages; it’s much more similar to C++ templates or Java generics.
Datatypes and pattern matching, which are ubiquitous in ML programming (and catching on in many other languages too, e.g., Rust, Python, Java, C++).