~ubuntu-branches/ubuntu/hardy/ocaml-doc/hardy

« back to all changes in this revision

Viewing changes to examples/basics/fra.html

  • Committer: Bazaar Package Importer
  • Author(s): Samuel Mimram
  • Date: 2007-09-08 01:49:22 UTC
  • mfrom: (0.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070908014922-lvihyehz0ndq7suu
Tags: 3.10-1
* New upstream release.
* Removed camlp4 documentation since it is not up-to-date.
* Updated to standards version 3.7.2, no changes needed.
* Updated my email address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//FR"
2
 
            "http://www.w3.org/TR/REC-html40/loose.dtd">
3
 
<HTML>
4
 
<HEAD>
5
 
<TITLE>Caml Examples</TITLE>
6
 
</HEAD>
7
 
 
8
 
<BODY BGCOLOR="#FFFFFF">
9
 
 
10
 
<P>File created 2 February 2001.
11
 
 
12
 
<H1 ALIGN=CENTER><IMG SRC="../../gifs/JoeCaml.gif" ALT="">Simple and basic programs</H1>
13
 
 
14
 
<H2 ALIGN=LEFT>Running the programs</H2>
15
 
 
16
 
<P>
17
 
Calling the Caml compiler:
18
 
 
19
 
<UL>
20
 
<LI>to compile the file hello.ml to executable program a.out type<BR>
21
 
    ocamlc hello.ml
22
 
<LI>to compile the file hello.ml to executable program hello type<BR>
23
 
    ocamlc -o hello hello.ml
24
 
</UL>
25
 
 
26
 
<P>
27
 
To try interactively: call the Caml interactive system
28
 
 
29
 
<PRE>
30
 
        ocaml               # or better, ledit ocaml, if ledit is installed.
31
 
</PRE>
32
 
 
33
 
Then type in (don't forget the initial <CODE>#</CODE> sign, that
34
 
indicates a directive)
35
 
<PRE>
36
 
        #use "loadall.ml";;
37
 
</PRE>
38
 
 
39
 
<H2 ALIGN=LEFT>The programs</H2>
40
 
 
41
 
This directory contains the following programs:
42
 
 
43
 
<H3 ALIGN=LEFT>Basic programs</H3>
44
 
 
45
 
<P>Bla bla, non ?
46
 
 <A HREF="hello.ml"><CODE>hello.ml</CODE></A>.
47
 
 
48
 
<DL>
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>
52
 
  <DD>Try<BR>
53
 
       <CODE>hello</CODE>
54
 
  </DD>
55
 
 
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
59
 
  user and die.</DD>
60
 
  <DD>Try<BR>
61
 
       <CODE>greeting</CODE>
62
 
  </DD>
63
 
 
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>
68
 
  <DD>Try<BR>
69
 
       <CODE>argcargv 1 Camel</CODE>
70
 
  </DD>
71
 
 
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>
76
 
  <DD>Try<BR>
77
 
       <CODE>square 16</CODE>
78
 
  </DD>
79
 
 
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>
83
 
  <DD>Try<BR>
84
 
       <CODE>fib 10</CODE>
85
 
  </DD>
86
 
 
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>
91
 
  <DD>Try<BR>
92
 
       <CODE>./wc wc.ml</CODE>
93
 
  </DD>
94
 
 
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>
98
 
  <DD>Try<BR>
99
 
       <CODE>./wc_unix *.ml</CODE>
100
 
  </DD>
101
 
 
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>
106
 
  <DD>Try<BR>
107
 
       <CODE>reverse_stdin &lt; reverse_stdin.ml</CODE>
108
 
  </DD>
109
 
 
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>
114
 
  <DD>Try<BR>
115
 
       <CODE>reverse_rec &lt; reverse_stdin.ml</CODE>
116
 
  </DD>
117
 
 
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.
122
 
  <BR>Uses lists.</DD>
123
 
  <DD>Try<BR>
124
 
       <CODE>sieve 1000</CODE>
125
 
  </DD>
126
 
 
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
131
 
  integer argument. 
132
 
  <BR>Uses and manipulates vectors.</DD>
133
 
  <DD>Try<BR>
134
 
       <CODE>sieve_vect 1000</CODE>
135
 
  </DD>
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).
142
 
  </DD>
143
 
 
144
 
 
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>
151
 
  <DD>Try<BR>
152
 
       <CODE>queens 8</CODE>
153
 
  </DD>
154
 
 
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>
159
 
  <DD>Try<BR>
160
 
       <CODE>soli/CODE>
161
 
  </DD>
162
 
</DL>
163
 
 
164
 
<H3 ALIGN=LEFT>Simple library modules</H3>
165
 
 
166
 
<DL>
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).
176
 
  </DD>
177
 
 
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.
186
 
  </DD>
187
 
 
188
 
</DL>
189
 
 
190
 
<H3 ALIGN=LEFT>Advanced programs</H3>
191
 
 
192
 
<DL>
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
197
 
  occurrence.
198
 
  <BR>Uses recursive function programming to implement a naive algorithm.</DD>
199
 
  <DD>Try<BR>
200
 
       <PRE>
201
 
        strpos rs strstr
202
 
        strpos ra strstr
203
 
        </PRE>
204
 
  </DD>
205
 
 
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
210
 
  occurrence.
211
 
  <BR>Uses imperative programming, while loops and references to
212
 
  implement the Knuth-Morris-Pratt algorithm.</DD>
213
 
  <DD>Try<BR>
214
 
       <PRE>
215
 
        kmp rs strstr
216
 
        kmp ra strstr
217
 
        </PRE>
218
 
  </DD>
219
 
 
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.
225
 
  </DD>
226
 
  <DD>Try<BR>
227
 
       <PRE>
228
 
        queens_tail 8
229
 
        </PRE>
230
 
  </DD>
231
 
 
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.
239
 
  </DD>
240
 
  <DD>Try<BR>
241
 
       <PRE>
242
 
        queens_lazy 8
243
 
        </PRE>
244
 
  </DD>
245
 
 
246
 
</DL>
247
 
 
248
 
 
249
 
<H2 ALIGN=LEFT>Automatic recompilation</H2>
250
 
 
251
 
<P>
252
 
To compile: either type "make", or, by hand:
253
 
<PRE>
254
 
        ocamlc -o fib fib.ml
255
 
        ocamlc -o wc wc.ml
256
 
        ocamlc -o sieve sieve.ml
257
 
</PRE>
258
 
 
259
 
<P>To run:
260
 
 
261
 
<PRE>
262
 
        fib 10              # or some other number
263
 
        wc fib.ml           # or some other files
264
 
        sieve 1000          # or some other number
265
 
</PRE>
266
 
 
267
 
<P>To compile to native code: either "make opt", or, by hand:
268
 
 
269
 
<PRE>
270
 
        ocamlopt -o fib fib.ml
271
 
        ocamlopt -o wc wc.ml
272
 
        ocamlopt -o sieve sieve.ml
273
 
</PRE>
274
 
 
275
 
<P>To try interactively:
276
 
 
277
 
<PRE>
278
 
        ocaml               # or ledit ocaml if ledit is installed.
279
 
        #use "loadall.ml";;
280
 
</PRE>
281
 
 
282
 
<P>
283
 
<ADDRESS>Contact the author <A HREF="mailto:Pierre.Weis@inria.fr">Pierre.Weis@inria.fr</A></ADDRESS>
284
 
<HR>
285
 
 
286
 
</BODY>
287
 
</HTML>
288