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

« back to all changes in this revision

Viewing changes to examples/basics/README

  • 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
 
Extremely simple and basic programs.
2
 
 
3
 
Calling the Caml compiler:
4
 
  to compile the file hello.ml to executable program a.out type
5
 
    ocamlc hello.ml
6
 
  to compile the file hello.ml to executable program hello type
7
 
    ocamlc -o hello hello.ml
8
 
 
9
 
To try interactively:
10
 
 
11
 
        ocaml               # or ledit ocaml if ledit is installed.
12
 
        #use "loadall.ml";;
13
 
 
14
 
This directory contains the following programs:
15
 
 
16
 
Basic programs:
17
 
 
18
 
 Hello: source programm is in file hello.ml.
19
 
  Just prints Hello world! followed by a newline.
20
 
  Try
21
 
       hello
22
 
 
23
 
 Greeting: source programm is in file greeting.ml.
24
 
  Ask the name of the user, reads the input at keyboard, greets the
25
 
  user and die.
26
 
  Try
27
 
       greeting
28
 
 
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.
34
 
  Try
35
 
       argcargv
36
 
       argcargv 1 2
37
 
       argcargv 1 2 "ok" -f "/tmp/foo"
38
 
 
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.
42
 
  Try
43
 
        square 16
44
 
 
45
 
 Fib: source program is in fib.ml.
46
 
  Define the Fibonacci function as a simple recursive Caml function.
47
 
  Try
48
 
        fib 10
49
 
 
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.
54
 
  Try
55
 
        ./wc wc.ml
56
 
 
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
60
 
  loops.
61
 
  Try
62
 
        reverse_stdin < reverse_stdin.ml
63
 
 
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.
68
 
  Try
69
 
        reverse_rec < reverse_rec.ml
70
 
 
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.
75
 
  Try
76
 
        ./wc_unix *.ml
77
 
 
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.
81
 
  Uses lists.
82
 
  Try
83
 
        sieve 1000
84
 
 
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.
90
 
  Try
91
 
        sieve_vect 1000
92
 
 
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).
96
 
 
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.
101
 
  Try
102
 
        queens 8
103
 
 
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.
107
 
  Try
108
 
        soli
109
 
 
110
 
Simple library modules
111
 
 
112
 
 Realloc: module Realloc, the source implementation of the module 
113
 
  is in file realloc.ml, the source interface of the module is in
114
 
  realloc.mli. 
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).
119
 
 
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.
124
 
 
125
 
Advanced examples:
126
 
 
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
130
 
  occurrence.
131
 
  Uses recursive functional programming to implement a naive algorithm.
132
 
  Try
133
 
        strpos rs strstr
134
 
        strpos ra strstr
135
 
 
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
139
 
  occurrence.
140
 
  Uses imperative programming, while loops and references to implement
141
 
  the Knuth-Morris-Pratt algorithm.
142
 
  Try
143
 
        kmp rs strstr
144
 
        kmp ra strstr
145
 
 
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.
150
 
  Try
151
 
        queens_tail 8
152
 
 
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.
158
 
  Try
159
 
        queens_lazy 8
160
 
 
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
163
 
sieve.ml, enter:
164
 
 
165
 
        ocamlc -o fib fib.ml
166
 
        ocamlc -o wc wc.ml
167
 
        ocamlc -o sieve sieve.ml
168
 
 
169
 
To run the executables:
170
 
 
171
 
        fib 10              # or some other number
172
 
        wc fib.ml           # or some other files
173
 
        sieve 1000          # or some other number
174
 
 
175
 
To compile to native code: either "make opt", or call the native code
176
 
compiler (ocamlopt) instead of ocamlc:
177
 
 
178
 
        ocamlopt -o fib fib.ml
179
 
        ocamlopt -o wc wc.ml
180
 
        ocamlopt -o sieve sieve.ml
181
 
 
182
 
Generally speaking, native code executable programs are bigger and
183
 
faster to execute.
184
 
 
185
 
To try interactively call the interactive system, and load the source
186
 
file loadall.ml:
187
 
 
188
 
        ocaml               # or ledit ocaml if ledit is installed.
189
 
        #use "loadall.ml";;
190