Introduction to Ocaml
Ocaml is installed on the CS machines at
/l/ocaml
. There are several ways to run Ocaml: the
simplest is to write your definitions in a single file and invoke the
top-level read-eval-print loop. As we move along and our programs get
larger, we will use modules spread over several files and use the
separate compilation and linking facilities. For now, if your
definitions are in a file like test.ml
, then you can load and
evaluate the definitions as follows:
> /l/ocaml/bin/ocaml
Objective Caml version 3.06
# #use "test.ml";;
val fact : int -> int =
# fact 5;;
- : int = 120
# exit 0;;
Assignment
- Install Ocaml on your personal computer or make sure you have
access to a CS machine. Ocaml is not yet installed on the campus-wide
network but should be available there soon.
- Write Ocaml definitions to the problems 1 to 8 that are part of
exercise 1.15 in the book. For problem number 6, raise an exception
instead of returning
#f
. Some of the examples use lists
with elements of different types; modify your test cases to use the
kinds of lists that Ocaml supports in which every element must have
the same type.