1
(***********************************************************************)
5
(* Pierre Weis, projet Cristal, INRIA Rocquencourt *)
7
(* Copyright 2001 Institut National de Recherche en Informatique et *)
8
(* en Automatique. All rights reserved. This file is distributed *)
9
(* only by permission. *)
11
(***********************************************************************)
14
- Variables exist in Caml.
15
A variable x is defined using the ``ref'' variable constructor
16
applied to its initial value (initial value is mandatory).
18
The variable can be modified using the assignment operator :=
27
- New type definitions are introduced by the keyword type. To define an
28
enumerated type, just list the set of alternatives.
29
Here type state introduced two cases, Inside_word and Outside_word,
30
that will serve3 to denote if we are scanning a word or not.
32
type state = Inside_word | Outside_word;;
35
- Case analysis is introduced by match. It is a list of clauses
37
meaning that if pat is the case at hand, e should be returned.
38
For instance, to return integer 1 if character c is 'a' and 2 if c
43
A catch all case is introduced by special pattern ``_''. Hence,
47
tests is character c is 'a'.
49
- Character can be read in input channel using primitive input_char.
51
- Primitive incr, increments a variable.
53
let count_channel in_channel =
54
let rec count status =
55
let c = input_char in_channel in
59
incr lines; count Outside_word
63
if status = Outside_word then incr words;
67
with End_of_file -> ()
71
- Primitive open_in opens an input channel.
74
let ic = open_in name in
80
- The current value of variable x is denoted by !x.
81
Hence incr x is equivalent to x := !x + 1
84
print_int !chars; print_string " characters, ";
85
print_int !words; print_string " words, ";
86
print_int !lines; print_string " lines";
95
if !Sys.interactive then () else
97
if Array.length Sys.argv <= 1 then
98
count_channel stdin (* No command-line arguments *)
100
for i = 1 to Array.length Sys.argv - 1 do
101
count_file Sys.argv.(i)
105
print_string "I/O error: ";