1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//FR"
2
"http://www.w3.org/TR/REC-html40/loose.dtd">
5
<TITLE>Caml Examples</TITLE>
8
<BODY BGCOLOR="#FFFFFF">
10
<P>File created 2 February 2001.
12
<H1 ALIGN=CENTER><IMG SRC="../../gifs/JoeCaml.gif" ALT="">Simple and basic programs</H1>
14
<H2 ALIGN=LEFT>How to compile and run the programs</H2>
16
<P>Calling the Caml compiler:
19
<LI>to compile the file hello.ml to executable program a.out type<BR>
21
<LI>to compile the file hello.ml to executable program hello type<BR>
22
ocamlc -o hello hello.ml
25
<P>To try interactively: call the Caml interactive system
28
ocaml # or better, ledit ocaml, if ledit is installed.
31
Then type in (don't forget the initial <CODE>#</CODE> sign, that
32
indicates a directive)
37
<H2 ALIGN=LEFT>Automatic recompilation</H2>
40
To compile: either type "make", or, by hand:
44
ocamlc -o sieve sieve.ml
50
fib 10 # or some other number
51
wc fib.ml # or some other files
52
sieve 1000 # or some other number
55
<P>To compile programs to native code: either "make opt", or, by hand:
58
ocamlopt -o fib fib.ml
60
ocamlopt -o sieve sieve.ml
63
<P>As mentioned above, you can also try the programs interactively:
66
ocaml # or ledit ocaml if ledit is installed.
70
<H2 ALIGN=LEFT>Basic programs</H2>
72
<P>This directory contains the following basic programs:
75
<DT><STRONG>Hello</STRONG>: the source program is in file
76
<A HREF="hello.ml"><CODE>hello.ml</CODE></A>.</DT>
77
<DD>Just prints Hello world! followed by a newline.</DD>
82
<DT><STRONG>Greeting</STRONG>: source program is in file
83
<A HREF="greeting.ml"><CODE>greeting.ml</CODE></A>.</DT>
84
<DD>Ask the name of the user, reads the input from the keyboard, greets the
90
<DT><STRONG>Argcargv</STRONG>: source program is in file
91
<A HREF="argcargv.ml"><CODE>argcargv.ml</CODE></A>.</DT>
92
<DD>Prints the number of arguments passed to the program on the
93
command line, then prints them all.</DD>
95
<CODE>argcargv 1 Camel</CODE>
98
<DT><STRONG>Square</STRONG>: source program is in file
99
<A HREF="square.ml"><CODE>square.ml</CODE></A>.</DT>
100
<DD>Reads an integer passed as argument to the program, then compute
101
and prints its square.</DD>
103
<CODE>square 16</CODE>
106
<DT><STRONG>Fib</STRONG>: source program is in file
107
<A HREF="fib.ml"><CODE>fib.ml</CODE></A>.</DT>
108
<DD>Define the Fibonacci function as a simple recursive Caml function.</DD>
113
<DT><STRONG>Wc</STRONG>: the source program is in file
114
<A HREF="wc.ml"><CODE>wc.ml</CODE></A>.</DT>
115
<DD>A program that mimicks the Unix "wc" utility: it counts the number of
116
characters, words, and lines of a given file.</DD>
118
<CODE>./wc wc.ml</CODE>
121
<DT><STRONG>Wc_unix</STRONG>: the source program is in file
122
<A HREF="wc_unix.ml"><CODE>wc_unix.ml</CODE></A>.</DT>
123
<DD>A Caml clone of the Unix "wc" utility.</DD>
125
<CODE>./wc_unix *.ml</CODE>
128
<DT><STRONG>Reverse_stdin</STRONG>: the source program is in file
129
<A HREF="reverse_stdin.ml"><CODE>reverse_stdin.ml</CODE></A>.</DT>
130
<DD>Reverse the lines reads from stdin.
131
<BR>Vectors and imperative programming with loops.</DD>
133
<CODE>reverse_stdin < reverse_stdin.ml</CODE>
136
<DT><STRONG>Reverse_rec</STRONG>: the source program is in file
137
<A HREF="reverse_rec.ml"><CODE>reverse_rec.ml</CODE></A>.</DT>
138
<DD>Reverse the lines reads from stdin.
139
<BR>Elegant recursive imperative programming.</DD>
141
<CODE>reverse_rec < reverse_stdin.ml</CODE>
144
<DT><STRONG>Sieve</STRONG>: the source program is in file
145
<A HREF="sieve.ml"><CODE>sieve.ml</CODE></A>.</DT>
146
<DD>The Eratosthene's sieve: the program computes the set of prime
147
numbers lesser than a given integer argument.
150
<CODE>sieve 1000</CODE>
153
<DT><STRONG>Sieve_vect</STRONG>: the source program is in file
154
<A HREF="sieve_vect.ml"><CODE>sieve_vect.ml</CODE></A>.</DT>
155
<DD>The Eratosthene's sieve in an imperative way, using a vector:
156
the program computes the number of prime numbers lesser than a given
158
<BR>Uses and manipulates vectors.</DD>
160
<CODE>sieve_vect 1000</CODE>
162
<DD><STRONG>Note</STRONG>: the C correspondant of
163
<CODE>sieve_vect.ml</CODE> is in file
164
<A HREF="sieve_vect.c"><CODE>sieve_vect.c</CODE></A>.
165
The Caml correspondant with maximum speed is in
166
<A HREF="sieve_vect_unsafe.ml"><CODE>sieve_vect_unsafe.ml</CODE></A>
167
(no array bound checks).
171
<DT><STRONG>Qeens</STRONG>: the source program is in file
172
<A HREF="queens.ml"><CODE>queens.ml</CODE></A>.</DT>
173
<DD>Lists manipulation: prints the solution to the 8 queens problem.</DD>
174
<DD>How to set n queens on a chessboard of size n such that none
175
can catch one each other.
176
<BR>Higher-order list manipulation.</DD>
178
<CODE>queens 8</CODE>
181
<DT><STRONG>Soli</STRONG>: the source program is in file
182
<A HREF="soli.ml"><CODE>soli.ml</CODE></A>.</DT>
183
<DD>Prints the solution to the famous ``solitaire'' game.
184
<BR>Vectors and data types definitions and manipulation.</DD>
190
<H2 ALIGN=LEFT>Simple library modules</H2>
192
<P>This directory contains two simple library module examples:
195
<DT><STRONG>Realloc</STRONG>: module Realloc, the source
196
implementation of the module is in file
197
<A HREF="realloc.ml"><CODE>realloc.ml</CODE></A>, the source
198
interface of the module is in file
199
<A HREF="realloc.mli"><CODE>realloc.mli</CODE></A>.</DT>
200
<DD>Defines a simple module to realloc (enlarge) arrays.
201
<BR>The module defines and exports a single realloc function.
202
<BR>Try to define and compile a program that uses realloc (for instance
203
to define dynamically extendable storage areas).
206
<DT><STRONG>Explode</STRONG>: module Explode, the source
207
implementation in file
208
<A HREF="explode.ml"><CODE>explode.ml</CODE></A>,
209
the interface is in file
210
<A HREF="explode.mli"><CODE>explode.mli</CODE></A>.</DT>
211
<DD>Defines explode and implode, two simple functions that convert a
212
string into a list of chars (explode) and converse (implode).
213
<BR>Those functons are linear and tail recursive.
218
<H2 ALIGN=LEFT>Advanced programs</H2>
220
<P>This directory contains the following less simple programs:
223
<DT><STRONG>Strpos</STRONG>: the source program is in file
224
<A HREF="strpos.ml"><CODE>strpos.ml</CODE></A>.</DT>
225
<DD>Tests if its first argument appears as a sub string of its second
226
argument, and returns the first character number of the first matching
228
<BR>Uses recursive function programming to implement a naive algorithm.</DD>
236
<DT><STRONG>Kmp</STRONG>: the source program is in file
237
<A HREF="kmp.ml"><CODE>kmp.ml</CODE></A>.</DT>
238
<DD>Tests if its first argument appears as a sub string of its second
239
argument, and returns the first character number of the first matching
241
<BR>Uses imperative programming, while loops and references to
242
implement the Knuth-Morris-Pratt algorithm.</DD>
250
<DT><STRONG>Qeens_tail</STRONG>: the source program is in file
251
<A HREF="queens_tail.ml"><CODE>queens_tail.ml</CODE></A>.</DT>
252
<DD>Same as Queens but the program is optimized, being written in a
253
so called ``tail rec'' style.
254
<BR>Interesting tail recursion exercise.
262
<DT><STRONG>Qeens_lazy</STRONG>: the source program is in file
263
<A HREF="queens_lazy.ml"><CODE>queens_lazy.ml</CODE></A>.</DT>
264
<DD>Same as Queens but the program is written in lazy style.
265
Lazyness is hand coded, hence extremely explicit.
266
Defines sum types to implement lazy lists, use mutable fields to
267
implement call by need.
268
<BR>Strange mixing of side effects and pure functionality.
279
<ADDRESS>Contact the author <A HREF="mailto:Pierre.Weis@inria.fr">Pierre.Weis@inria.fr</A></ADDRESS>