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

« back to all changes in this revision

Viewing changes to examples/basics/wc.ml

  • 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
 
(***********************************************************************)
2
 
(*                                                                     *)
3
 
(*                           Objective Caml                            *)
4
 
(*                                                                     *)
5
 
(*               Pierre Weis, projet Cristal, INRIA Rocquencourt       *)
6
 
(*                                                                     *)
7
 
(*  Copyright 2001 Institut National de Recherche en Informatique et   *)
8
 
(*  en Automatique.  All rights reserved.  This file is distributed    *)
9
 
(*  only by permission.                                                *)
10
 
(*                                                                     *)
11
 
(***********************************************************************)
12
 
 
13
 
(*
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).
17
 
    let x = ref 0
18
 
  The variable can be modified using the assignment operator :=
19
 
    x := 3
20
 
*)
21
 
let chars = ref 0
22
 
and words = ref 0
23
 
and lines = ref 0
24
 
;;
25
 
 
26
 
(*
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.
31
 
*)
32
 
type state = Inside_word | Outside_word;;
33
 
 
34
 
(*
35
 
- Case analysis is introduced by match. It is a list of clauses
36
 
  | pat -> e
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
39
 
   is 'b', use
40
 
   match c with
41
 
   | 'a' -> 1
42
 
   | 'b' -> 2
43
 
  A catch all case is introduced by special pattern ``_''. Hence,
44
 
  match c with
45
 
  | 'a' -> true
46
 
  | _ -> false
47
 
  tests is character c is 'a'.
48
 
 
49
 
- Character can be read in input channel using primitive input_char.
50
 
 
51
 
- Primitive incr, increments a variable.
52
 
*)
53
 
let count_channel in_channel =
54
 
  let rec count status =
55
 
    let c = input_char in_channel in
56
 
    incr chars;
57
 
    match c with
58
 
    | '\n' ->
59
 
        incr lines; count Outside_word
60
 
    | ' ' | '\t' ->
61
 
        count Outside_word
62
 
    | _ ->
63
 
        if status = Outside_word then incr words;
64
 
        count Inside_word in
65
 
  try
66
 
    count Outside_word
67
 
  with End_of_file -> ()
68
 
;;
69
 
 
70
 
(*
71
 
- Primitive open_in opens an input channel.
72
 
*)
73
 
let count_file name =
74
 
  let ic = open_in name in
75
 
  count_channel ic;
76
 
  close_in ic
77
 
;;
78
 
 
79
 
(*
80
 
- The current value of variable x is denoted by !x.
81
 
  Hence incr x is equivalent to x := !x + 1
82
 
*)
83
 
let print_result () =
84
 
  print_int !chars; print_string " characters, ";
85
 
  print_int !words; print_string " words, ";
86
 
  print_int !lines; print_string " lines";
87
 
  print_newline ()
88
 
;;
89
 
 
90
 
let count name =
91
 
  count_file name;
92
 
  print_result ()
93
 
;;  
94
 
 
95
 
if !Sys.interactive then () else
96
 
try
97
 
  if Array.length Sys.argv <= 1 then
98
 
    count_channel stdin                (* No command-line arguments *)
99
 
  else
100
 
    for i = 1 to Array.length Sys.argv - 1 do
101
 
      count_file Sys.argv.(i)
102
 
    done;
103
 
  print_result ();
104
 
with Sys_error s ->
105
 
  print_string "I/O error: ";
106
 
  print_string s;
107
 
  print_newline ()
108
 
;;