1
Extremely simple and basic programs.
3
Calling the Caml compiler:
4
to compile the file hello.ml to executable program a.out type
6
to compile the file hello.ml to executable program hello type
7
ocamlc -o hello hello.ml
11
ocaml # or ledit ocaml if ledit is installed.
14
This directory contains the following programs:
18
Hello: source programm is in file hello.ml.
19
Just prints Hello world! followed by a newline.
23
Greeting: source programm is in file greeting.ml.
24
Ask the name of the user, reads the input at keyboard, greets the
29
Argcargv: source program is in file argcargv.ml.
30
A simple program to determine the number of arguments and the
31
arguments passed to a Caml executable program.
32
Uses a simple for loop and the printf function to print the
33
vector of arguments passed to the command.
37
argcargv 1 2 "ok" -f "/tmp/foo"
39
Square: source program is in file square.ml
40
Reads an integer passed as argument to the program, then compute
41
and prints its square.
45
Fib: source program is in fib.ml.
46
Define the Fibonacci function as a simple recursive Caml function.
50
Wc: the source program is in wc.ml.
51
A program that mimicks the Unix "wc" utility: it counts the number of
52
characters, words, and lines of a given file.
53
Uses imperative variables or references.
57
Reverse_stdin: the source program is in reverse_stdin.ml.
58
A program to reverse the lines of a character stream given on standard input.
59
Basic references and array manipulation, recursive loops, and for
62
reverse_stdin < reverse_stdin.ml
64
Reverse_rec: the source program is in reverse_rec.ml.
65
Same specification as reverse_stdin.
66
Written in a more functional style, using recursive
67
functions. Extremely concise and elegant.
69
reverse_rec < reverse_rec.ml
71
Wc_unix: the source program is in wc_unix.ml.
72
A Caml clone of the Unix "wc" utility.
73
Defines an uses a new record type.
74
Introduces the printf formatting primitive.
78
Sieve: the source program is in sieve.ml.
79
The Eratosthene's sieve: the program computes the set of prime
80
numbers lesser than a given integer argument.
85
Sieve_vect: the source program is in sieve_vect.ml.
86
The Eratosthene's sieve in an imperative way, using a vector:
87
the program computes the number of prime
88
numbers lesser than a given integer argument.
89
Uses and manipulates vectors.
93
Note: the C correspondant of sieve_vect.ml is in sieve_vect.c.
94
The Caml correspondant with maximum speed is in sieve_vect_unsafe.ml
95
(no array bound checks).
97
Qeens: the source program is in queens.ml.
98
Lists manipulation: prints the solutions to the 8 queens problem.
99
How to set n queens on a chessboard of size n such that none
100
can catch one each other.
104
Soli: the source program is in soli.ml.
105
Prints the solution to the famous ``solitaire'' game.
106
Vectors and data types definitions and manipulation.
110
Simple library modules
112
Realloc: module Realloc, the source implementation of the module
113
is in file realloc.ml, the source interface of the module is in
115
Defines a simple module to realloc (enlarge) arrays.
116
The module defines and exports a single realloc function.
117
Try to define and compile a program that uses realloc (for instance
118
to define dynamically extendable storage areas).
120
Explode: implementation in explode.ml, interface in explode.mli.
121
Defines explode and implode two simple functions that convert a
122
string into a list of chars (explode) and converse (implode).
123
Those functons are linear and tail recursive.
127
Strpos: the source program is in strpos.ml.
128
Tests if its first argument appears as a sub string of its second
129
argument, and returns the character number of the first matching
131
Uses recursive functional programming to implement a naive algorithm.
136
Kmp: the source program is in kmp.ml.
137
Tests if its first argument appears as a sub string of its second
138
argument, and returns the character number of the first matching
140
Uses imperative programming, while loops and references to implement
141
the Knuth-Morris-Pratt algorithm.
146
Qeens_tail: the source program is in queens_tail.ml.
147
Same as Queens but the program is optimized, been written in a so
148
called ``tail rec'' style.
149
Interesting tail recursion exercise.
153
Qeens_lazy: the source program is in queens_lazy.ml.
154
Same as Queens but the program is written in lazy style.
155
Lazyness is hand coded hence extremely explicit.
156
Defines sum types to implement lazy lists, use mutable fields to
157
implement call by need.
161
To compile to byte code: either type "make", or enter directly the
162
compile commands. For instance, to byte compile fib.ml wc.ml and
167
ocamlc -o sieve sieve.ml
169
To run the executables:
171
fib 10 # or some other number
172
wc fib.ml # or some other files
173
sieve 1000 # or some other number
175
To compile to native code: either "make opt", or call the native code
176
compiler (ocamlopt) instead of ocamlc:
178
ocamlopt -o fib fib.ml
180
ocamlopt -o sieve sieve.ml
182
Generally speaking, native code executable programs are bigger and
185
To try interactively call the interactive system, and load the source
188
ocaml # or ledit ocaml if ledit is installed.