File created 2 February 2001.

Simple and basic programs

How to compile and run the programs

Calling the Caml compiler:

To try interactively: call the Caml interactive system

        ocaml               # or better, ledit ocaml, if ledit is installed.
Then type in (don't forget the initial # sign, that indicates a directive)
        #use "loadall.ml";;

Automatic recompilation

To compile: either type "make", or, by hand:

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

To run, type:

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

To compile programs to native code: either "make opt", or, by hand:

        ocamlopt -o fib fib.ml
        ocamlopt -o wc wc.ml
        ocamlopt -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";;

Basic programs

This directory contains the following basic programs:

Hello: the source program is in file hello.ml.
Just prints Hello world! followed by a newline.
Try
hello
Greeting: source program is in file greeting.ml.
Ask the name of the user, reads the input from the keyboard, greets the user and die.
Try
greeting
Argcargv: source program is in file argcargv.ml.
Prints the number of arguments passed to the program on the command line, then prints them all.
Try
argcargv 1 Camel
Square: source program is in file square.ml.
Reads an integer passed as argument to the program, then compute and prints its square.
Try
square 16
Fib: source program is in file fib.ml.
Define 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.
A Caml clone of the Unix "wc" utility.
Try
./wc_unix *.ml
Reverse_stdin: the source program is in file reverse_stdin.ml.
Reverse the lines reads 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.
Reverse the lines reads 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.
Prints the 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 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 number 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 number 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