File created 2 February 2001.

Simple and basic programs

How to compile the programs

Calling the Caml compiler:

How to run the programs

To run the compiled programs, use them as ordinary commands.

For instance, to try the Caml programs fib, wc, or sieve, type in:

        fib 10              # or some other number
        wc fib.ml           # or some other files
        sieve 1000          # or some other number

How to try and test the programs with the interactive system

To try the programs interactively: call the Caml interactive system and load the programs using the #load directive.

        ocaml               # or better, ledit ocaml, if ledit is installed.

Then type in (don't forget to type the initial # sign, that indicates a directive)

        #use "fib.ml";;

In sub directories that contain complex examples, a Caml source file named loadall.ml is provided. In this case just type:

        #use "loadall.ml";;

Automatic recompilation

In sub directories that contain complex examples, a Makefile is provided to automatically compile the set of Caml source files needed to compile and link the program.

To compile and link the program: either type "make", or, type manually the output of the make command:

        ocamlc -o fib fib.ml
        ocamlc -o wc wc.ml
        ocamlc -o sieve sieve.ml

As mentioned above, you can also try the programs interactively:

        ocaml               # or ledit ocaml if ledit is installed.
        #use "loadall.ml";;

Native code compilation

To compile the programs to native code: either type "make opt" is a Makefile is provided, or, type manually the suitable commmands:

        ocamlopt -o fib fib.ml
        ocamlopt -o wc wc.ml
        ocamlopt -o sieve sieve.ml

Basic programs

This directory contains the following basic programs:

Hello: the source program is in file hello.ml.
It just prints "Hello world!" followed by a newline.
Try
hello
Greeting: the source program is in file greeting.ml.
It asks the name of the user, reads the input from the keyboard, greets the user and dies.
Try
greeting
Argcargv: the source program is in file argcargv.ml.
It prints the number of arguments passed to the program on the command line, then prints them all.
Try
argcargv 1 Camel
Square: the source program is in file square.ml.
It reads an integer passed as argument to the program, then computes and prints the square of the integer.
Try
square 16
Fib: the source program is in file fib.ml.
It defines the Fibonacci function as a simple recursive Caml function.
Try
fib 10
Wc: the source program is in file wc.ml.
A program that mimicks the Unix "wc" utility: it counts the number of characters, words, and lines of a given file.
Try
./wc wc.ml
Wc_unix: the source program is in file wc_unix.ml.
This is a Caml clone of the Unix "wc" utility.
Try
./wc_unix *.ml
Reverse_stdin: the source program is in file reverse_stdin.ml.
It reverses the lines read from stdin.
Vectors and imperative programming with loops.
Try
reverse_stdin < reverse_stdin.ml
Reverse_rec: the source program is in file reverse_rec.ml.
Also reverses the lines read from stdin.
Elegant recursive imperative programming.
Try
reverse_rec < reverse_stdin.ml
Sieve: the source program is in file sieve.ml.
The Eratosthene's sieve: the program computes the set of prime numbers lesser than a given integer argument.
Uses lists.
Try
sieve 1000
Sieve_vect: the source program is in file sieve_vect.ml.
The Eratosthene's sieve in an imperative way, using a vector: the program computes the number of prime numbers lesser than a given integer argument.
Uses and manipulates vectors.
Try
sieve_vect 1000
Note: the C correspondant of sieve_vect.ml is in file sieve_vect.c. The Caml correspondant with maximum speed is in sieve_vect_unsafe.ml (no array bound checks).
Qeens: the source program is in file queens.ml.
Lists manipulation: prints the solution to the 8 queens problem.
How to set n queens on a chessboard of size n such that none can catch one each other.
Higher-order list manipulation.
Try
queens 8
Soli: the source program is in file soli.ml.
It prints a solution to the famous ``solitaire'' game.
Vectors and data types definitions and manipulation.
Try
soli

Simple library modules

This directory contains two simple library module examples:

Realloc: module Realloc, the source implementation of the module is in file realloc.ml, the source interface of the module is in file realloc.mli.
Defines a simple module to realloc (enlarge) arrays.
The module defines and exports a single realloc function.
Try to define and compile a program that uses realloc (for instance to define dynamically extendable storage areas).
Explode: module Explode, the source implementation is in file explode.ml, the interface is in file explode.mli.
Defines explode and implode, two simple functions that convert a string into a list of chars (explode) and converse (implode).
Those functons are linear and tail recursive.

Advanced programs

This directory contains the following less simple programs:

Strpos: the source program is in file strpos.ml.
Tests if its first argument appears as a sub string of its second argument, and returns the first character index of the first matching occurrence.
Uses recursive function programming to implement a naive algorithm.
Try
        strpos rs strstr
        strpos ra strstr
        
Kmp: the source program is in file kmp.ml.
Tests if its first argument appears as a sub string of its second argument, and returns the first character index of the first matching occurrence.
Uses imperative programming, while loops and references to implement the Knuth-Morris-Pratt algorithm.
Try
        kmp rs strstr
        kmp ra strstr
        
Qeens_tail: the source program is in file queens_tail.ml.
Same as Queens but the program is optimized, being written in a so called ``tail rec'' style.
Interesting tail recursion exercise.
Try
        queens_tail 8
        
Qeens_lazy: the source program is in file queens_lazy.ml.
Same as Queens but the program is written in lazy style. Lazyness is hand coded, hence extremely explicit. Defines sum types to implement lazy lists, use mutable fields to implement call by need.
Strange mixing of side effects and pure functionality.
Try
        queens_lazy 8
        

Contact the author Pierre.Weis@inria.fr