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

« back to all changes in this revision

Viewing changes to examples/basics/eng.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>How to compile and run the programs</H2>
15
 
 
16
 
<P>Calling the Caml compiler:
17
 
 
18
 
<UL>
19
 
<LI>to compile the file hello.ml to executable program a.out type<BR>
20
 
    ocamlc hello.ml
21
 
<LI>to compile the file hello.ml to executable program hello type<BR>
22
 
    ocamlc -o hello hello.ml
23
 
</UL>
24
 
 
25
 
<P>To try interactively: call the Caml interactive system
26
 
 
27
 
<PRE>
28
 
        ocaml               # or better, ledit ocaml, if ledit is installed.
29
 
</PRE>
30
 
 
31
 
Then type in (don't forget the initial <CODE>#</CODE> sign, that
32
 
indicates a directive)
33
 
<PRE>
34
 
        #use "loadall.ml";;
35
 
</PRE>
36
 
 
37
 
<H2 ALIGN=LEFT>Automatic recompilation</H2>
38
 
 
39
 
<P>
40
 
To compile: either type "make", or, by hand:
41
 
<PRE>
42
 
        ocamlc -o fib fib.ml
43
 
        ocamlc -o wc wc.ml
44
 
        ocamlc -o sieve sieve.ml
45
 
</PRE>
46
 
 
47
 
<P>To run, type:
48
 
 
49
 
<PRE>
50
 
        fib 10              # or some other number
51
 
        wc fib.ml           # or some other files
52
 
        sieve 1000          # or some other number
53
 
</PRE>
54
 
 
55
 
<P>To compile programs to native code: either "make opt", or, by hand:
56
 
 
57
 
<PRE>
58
 
        ocamlopt -o fib fib.ml
59
 
        ocamlopt -o wc wc.ml
60
 
        ocamlopt -o sieve sieve.ml
61
 
</PRE>
62
 
 
63
 
<P>As mentioned above, you can also try the programs interactively:
64
 
 
65
 
<PRE>
66
 
        ocaml               # or ledit ocaml if ledit is installed.
67
 
        #use "loadall.ml";;
68
 
</PRE>
69
 
 
70
 
<H2 ALIGN=LEFT>Basic programs</H2>
71
 
 
72
 
<P>This directory contains the following basic programs:
73
 
 
74
 
<DL>
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>
78
 
  <DD>Try<BR>
79
 
       <CODE>hello</CODE>
80
 
  </DD>
81
 
 
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
85
 
  user and die.</DD>
86
 
  <DD>Try<BR>
87
 
       <CODE>greeting</CODE>
88
 
  </DD>
89
 
 
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>
94
 
  <DD>Try<BR>
95
 
       <CODE>argcargv 1 Camel</CODE>
96
 
  </DD>
97
 
 
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>
102
 
  <DD>Try<BR>
103
 
       <CODE>square 16</CODE>
104
 
  </DD>
105
 
 
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>
109
 
  <DD>Try<BR>
110
 
       <CODE>fib 10</CODE>
111
 
  </DD>
112
 
 
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>
117
 
  <DD>Try<BR>
118
 
       <CODE>./wc wc.ml</CODE>
119
 
  </DD>
120
 
 
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>
124
 
  <DD>Try<BR>
125
 
       <CODE>./wc_unix *.ml</CODE>
126
 
  </DD>
127
 
 
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>
132
 
  <DD>Try<BR>
133
 
       <CODE>reverse_stdin &lt; reverse_stdin.ml</CODE>
134
 
  </DD>
135
 
 
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>
140
 
  <DD>Try<BR>
141
 
       <CODE>reverse_rec &lt; reverse_stdin.ml</CODE>
142
 
  </DD>
143
 
 
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.
148
 
  <BR>Uses lists.</DD>
149
 
  <DD>Try<BR>
150
 
       <CODE>sieve 1000</CODE>
151
 
  </DD>
152
 
 
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
157
 
  integer argument. 
158
 
  <BR>Uses and manipulates vectors.</DD>
159
 
  <DD>Try<BR>
160
 
       <CODE>sieve_vect 1000</CODE>
161
 
  </DD>
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).
168
 
  </DD>
169
 
 
170
 
 
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>
177
 
  <DD>Try<BR>
178
 
       <CODE>queens 8</CODE>
179
 
  </DD>
180
 
 
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>
185
 
  <DD>Try<BR>
186
 
       <CODE>soli</CODE>
187
 
  </DD>
188
 
</DL>
189
 
 
190
 
<H2 ALIGN=LEFT>Simple library modules</H2>
191
 
 
192
 
<P>This directory contains two simple library module examples:
193
 
 
194
 
<DL>
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).
204
 
  </DD>
205
 
 
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.
214
 
  </DD>
215
 
 
216
 
</DL>
217
 
 
218
 
<H2 ALIGN=LEFT>Advanced programs</H2>
219
 
 
220
 
<P>This directory contains the following less simple programs:
221
 
 
222
 
<DL>
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
227
 
  occurrence.
228
 
  <BR>Uses recursive function programming to implement a naive algorithm.</DD>
229
 
  <DD>Try<BR>
230
 
       <PRE>
231
 
        strpos rs strstr
232
 
        strpos ra strstr
233
 
        </PRE>
234
 
  </DD>
235
 
 
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
240
 
  occurrence.
241
 
  <BR>Uses imperative programming, while loops and references to
242
 
  implement the Knuth-Morris-Pratt algorithm.</DD>
243
 
  <DD>Try<BR>
244
 
       <PRE>
245
 
        kmp rs strstr
246
 
        kmp ra strstr
247
 
        </PRE>
248
 
  </DD>
249
 
 
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.
255
 
  </DD>
256
 
  <DD>Try<BR>
257
 
       <PRE>
258
 
        queens_tail 8
259
 
        </PRE>
260
 
  </DD>
261
 
 
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.
269
 
  </DD>
270
 
  <DD>Try<BR>
271
 
       <PRE>
272
 
        queens_lazy 8
273
 
        </PRE>
274
 
  </DD>
275
 
 
276
 
</DL>
277
 
 
278
 
<P>
279
 
<ADDRESS>Contact the author <A HREF="mailto:Pierre.Weis@inria.fr">Pierre.Weis@inria.fr</A></ADDRESS>
280
 
<HR>
281
 
 
282
 
</BODY>
283
 
</HTML>
284