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>Running the programs</H2>
17
Calling the Caml compiler:
20
<LI>to compile the file hello.ml to executable program a.out type<BR>
22
<LI>to compile the file hello.ml to executable program hello type<BR>
23
ocamlc -o hello hello.ml
27
To try interactively: call the Caml interactive system
30
ocaml # or better, ledit ocaml, if ledit is installed.
33
Then type in (don't forget the initial <CODE>#</CODE> sign, that
34
indicates a directive)
39
<H2 ALIGN=LEFT>The programs</H2>
41
This directory contains the following programs:
43
<H3 ALIGN=LEFT>Basic programs</H3>
46
<A HREF="hello.ml"><CODE>hello.ml</CODE></A>.
49
<DT><STRONG>Hello</STRONG>: the source program is in file
50
<A HREF="hello.ml"><CODE>hello.ml</CODE></A>.</DT>
51
<DD>Just prints Hello world! followed by a newline.</DD>
56
<DT><STRONG>Greeting</STRONG>: the source program is in file
57
<A HREF="greeting.ml"><CODE>greeting.ml</CODE></A>.</DT>
58
<DD>Ask the name of the user, reads the input from the keyboard, greets the
64
<DT><STRONG>Argcargv</STRONG>: the source program is in file
65
<A HREF="argcargv.ml"><CODE>argcargv.ml</CODE></A>.</DT>
66
<DD>Prints the number of arguments passed to the program on the
67
command line, then prints them all.</DD>
69
<CODE>argcargv 1 Camel</CODE>
72
<DT><STRONG>Square</STRONG>: the source program is in file
73
<A HREF="square.ml"><CODE>square.ml</CODE></A>.</DT>
74
<DD>Reads an integer passed as argument to the program, then compute
75
and prints its square.</DD>
77
<CODE>square 16</CODE>
80
<DT><STRONG>Fib</STRONG>: the source program is in file
81
<A HREF="fib.ml"><CODE>fib.ml</CODE></A>.</DT>
82
<DD>Define the Fibonacci function as a simple recursive Caml function.</DD>
87
<DT><STRONG>Wc</STRONG>: the source program is in file
88
<A HREF="wc.ml"><CODE>wc.ml</CODE></A>.</DT>
89
<DD>A program that mimicks the Unix "wc" utility: it counts the number of
90
characters, words, and lines of a given file.</DD>
92
<CODE>./wc wc.ml</CODE>
95
<DT><STRONG>Wc_unix</STRONG>: the source program is in file
96
<A HREF="wc_unix.ml"><CODE>wc_unix.ml</CODE></A>.</DT>
97
<DD>A Caml clone of the Unix "wc" utility.</DD>
99
<CODE>./wc_unix *.ml</CODE>
102
<DT><STRONG>Reverse_stdin</STRONG>: the source program is in file
103
<A HREF="reverse_stdin.ml"><CODE>reverse_stdin.ml</CODE></A>.</DT>
104
<DD>Reverse the lines reads from stdin.
105
<BR>Vectors and imperative programming with loops.</DD>
107
<CODE>reverse_stdin < reverse_stdin.ml</CODE>
110
<DT><STRONG>Reverse_rec</STRONG>: the source program is in file
111
<A HREF="reverse_rec.ml"><CODE>reverse_rec.ml</CODE></A>.</DT>
112
<DD>Reverse the lines reads from stdin.
113
<BR>Elegant recursive imperative programming.</DD>
115
<CODE>reverse_rec < reverse_stdin.ml</CODE>
118
<DT><STRONG>Sieve</STRONG>: the source program is in file
119
<A HREF="sieve.ml"><CODE>sieve.ml</CODE></A>.</DT>
120
<DD>The Eratosthene's sieve: the program computes the set of prime
121
numbers lesser than a given integer argument.
124
<CODE>sieve 1000</CODE>
127
<DT><STRONG>Sieve_vect</STRONG>: the source program is in file
128
<A HREF="sieve_vect.ml"><CODE>sieve_vect.ml</CODE></A>.</DT>
129
<DD>The Eratosthene's sieve in an imperative way, using a vector:
130
the program computes the number of prime numbers lesser than a given
132
<BR>Uses and manipulates vectors.</DD>
134
<CODE>sieve_vect 1000</CODE>
136
<DD><STRONG>Note</STRONG>: the C correspondant of
137
<CODE>sieve_vect.ml</CODE> is in file
138
<A HREF="sieve_vect.c"><CODE>sieve_vect.c</CODE></A>.
139
The Caml correspondant with maximum speed is in
140
<A HREF="sieve_vect_unsafe.ml"><CODE>sieve_vect_unsafe.ml</CODE></A>
141
(no array bound checks).
145
<DT><STRONG>Qeens</STRONG>: the source program is in file
146
<A HREF="queens.ml"><CODE>queens.ml</CODE></A>.</DT>
147
<DD>Lists manipulation: prints the solution to the 8 queens problem.</DD>
148
<DD>How to set n queens on a chessboard of size n such that none
149
can catch one each other.
150
<BR>Higher-order list manipulation.</DD>
152
<CODE>queens 8</CODE>
155
<DT><STRONG>Soli</STRONG>: the source program is in file
156
<A HREF="soli.ml"><CODE>soli.ml</CODE></A>.</DT>
157
<DD>Prints the solution to the famous ``solitaire'' game.
158
<BR>Vectors and data types definitions and manipulation.</DD>
164
<H3 ALIGN=LEFT>Simple library modules</H3>
167
<DT><STRONG>Realloc</STRONG>: module Realloc, the source
168
implementation of the module is in file
169
<A HREF="realloc.ml"><CODE>realloc.ml</CODE></A>, the source
170
interface of the module is in file
171
<A HREF="realloc.mli"><CODE>realloc.mli</CODE></A>.</DT>
172
<DD>Defines a simple module to realloc (enlarge) arrays.
173
<BR>The module defines and exports a single realloc function.
174
<BR>Try to define and compile a program that uses realloc (for instance
175
to define dynamically extendable storage areas).
178
<DT><STRONG>Explode</STRONG>: module Explode, the source
179
implementation in file
180
<A HREF="explode.ml"><CODE>explode.ml</CODE></A>,
181
the interface is in file
182
<A HREF="explode.mli"><CODE>explode.mli</CODE></A>.</DT>
183
<DD>Defines explode and implode, two simple functions that convert a
184
string into a list of chars (explode) and converse (implode).
185
<BR>Those functons are linear and tail recursive.
190
<H3 ALIGN=LEFT>Advanced programs</H3>
193
<DT><STRONG>Strpos</STRONG>: the source program is in file
194
<A HREF="strpos.ml"><CODE>strpos.ml</CODE></A>.</DT>
195
<DD>Tests if its first argument appears as a sub string of its second
196
argument, and returns the first character number of the first matching
198
<BR>Uses recursive function programming to implement a naive algorithm.</DD>
206
<DT><STRONG>Kmp</STRONG>: the source program is in file
207
<A HREF="kmp.ml"><CODE>kmp.ml</CODE></A>.</DT>
208
<DD>Tests if its first argument appears as a sub string of its second
209
argument, and returns the first character number of the first matching
211
<BR>Uses imperative programming, while loops and references to
212
implement the Knuth-Morris-Pratt algorithm.</DD>
220
<DT><STRONG>Qeens_tail</STRONG>: the source program is in file
221
<A HREF="queens_tail.ml"><CODE>queens_tail.ml</CODE></A>.</DT>
222
<DD>Same as Queens but the program is optimized, being written in a
223
so called ``tail rec'' style.
224
<BR>Interesting tail recursion exercise.
232
<DT><STRONG>Qeens_lazy</STRONG>: the source program is in file
233
<A HREF="queens_lazy.ml"><CODE>queens_lazy.ml</CODE></A>.</DT>
234
<DD>Same as Queens but the program is written in lazy style.
235
Lazyness is hand coded, hence extremely explicit.
236
Defines sum types to implement lazy lists, use mutable fields to
237
implement call by need.
238
<BR>Strange mixing of side effects and pure functionality.
249
<H2 ALIGN=LEFT>Automatic recompilation</H2>
252
To compile: either type "make", or, by hand:
256
ocamlc -o sieve sieve.ml
262
fib 10 # or some other number
263
wc fib.ml # or some other files
264
sieve 1000 # or some other number
267
<P>To compile to native code: either "make opt", or, by hand:
270
ocamlopt -o fib fib.ml
272
ocamlopt -o sieve sieve.ml
275
<P>To try interactively:
278
ocaml # or ledit ocaml if ledit is installed.
283
<ADDRESS>Contact the author <A HREF="mailto:Pierre.Weis@inria.fr">Pierre.Weis@inria.fr</A></ADDRESS>