File created 2 February 2001.
Extremely simple and basic programs
Running the programs
Calling the Caml compiler:
- to compile the file hello.ml to executable program a.out type
ocamlc hello.ml
- to compile the file hello.ml to executable program hello type
ocamlc -o hello hello.ml
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";;
The programs
This directory contains the following programs:
Basic programs
- Hello: source programm is in file hello.ml.
- Just prints Hello world! followed by a newline.
- Try
hello
- Greeting: source programm 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 fib.ml.
- Define the Fibonacci function as a simple recursive Caml function.
- Try
fib 10
- Wc: the source program is in 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 wc_unix.ml.
- A Caml clone of the Unix "wc" utility.
- Try
./wc_unix *.ml
- Reverse_stdin: the source program is in 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 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 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 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 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 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 soli.ml.
- Prints the solution to the famous ``solitaire'' game.
Vectors and data types definitions and manipulation.
- Try
soli/CODE>
Simple library modules
- Realloc: module Realloc, the source
implementation of the module is in file realloc.ml, the source
interface of the module is in 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 explode.ml, the interface in 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
- Strpos: the source program is in strpos.ml.
- Tests if its first argument appears as a sub string of its second
argument, and returns the character number of the first matching
occurrence.
Uses function recursive programming to implement a naive algorithm.
- Try
strpos rs strstr
strpos ra strstr
- Kmp: the source program is in
kmp.ml
.
- Tests if its first argument appears as a sub string of its second
argument, and returns the 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 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 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
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:
fib 10 # or some other number
wc fib.ml # or some other files
sieve 1000 # or some other number
To compile to native code: either "make opt", or, by hand:
ocamlopt -o fib fib.ml
ocamlopt -o wc wc.ml
ocamlopt -o sieve sieve.ml
To try interactively:
ocaml # or ledit ocaml if ledit is installed.
#use "loadall.ml";;
Contact the author Pierre.Weis@inria.fr