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

« back to all changes in this revision

Viewing changes to examples/basics/sieve.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
 
  Manipulation of lists.
14
 
 
15
 
  - [] is the empty list
16
 
  - :: is the infix operator that builds list cells.
17
 
    Hence 1 :: (2 :: []) is the list that contains 1 and 2 in this order.
18
 
 
19
 
*)
20
 
(* interval min max = [min; min+1; ...; max-1; max] *)
21
 
 
22
 
let rec interval min max =
23
 
  if min > max then [] else min :: interval (succ min) max
24
 
;;
25
 
 
26
 
(*
27
 
 
28
 
  Case analysis on list l is written
29
 
  match l with
30
 
  | [] -> ``nil case''
31
 
  | x :: tail -> ``non nil case,
32
 
                   with x (the head of l) and tail (the tail of l) available''
33
 
 
34
 
  Function can perform direct case analysis on their argument,
35
 
  if introduced by the function keyword. Hence,
36
 
 
37
 
    let f (x) =
38
 
      match x with
39
 
      | [] -> ...
40
 
      | ...
41
 
 
42
 
  can be abreviated as
43
 
 
44
 
    let f = function
45
 
      | [] -> ...
46
 
      | ...
47
 
 
48
 
*)
49
 
 
50
 
(* filter p L returns the list of the elements in list L
51
 
   that satisfy predicate p *)
52
 
 
53
 
let rec filter p = function
54
 
  | []  -> []
55
 
  | a :: r -> if p a then a :: filter p r else filter p r
56
 
;;
57
 
 
58
 
(* Application: removing all numbers multiple of n from a list of integers *)
59
 
 
60
 
let remove_multiples_of n =
61
 
  filter (fun m -> m mod n <> 0)
62
 
;;
63
 
 
64
 
(* The sieve itself *)
65
 
 
66
 
let sieve max =
67
 
  let rec filter_again = function
68
 
  | [] -> []
69
 
  | n :: r as l ->
70
 
      if n * n > max then l else n :: filter_again (remove_multiples_of n r)
71
 
  in
72
 
    filter_again (interval 2 max)
73
 
;;
74
 
 
75
 
let usage() =
76
 
  print_string "Usage: sieve <n>\n";
77
 
  exit 2
78
 
;;
79
 
 
80
 
(* The entry point *)
81
 
 
82
 
let main () =
83
 
  if Array.length Sys.argv <> 2 then usage () else
84
 
  begin
85
 
    try
86
 
      let n = int_of_string Sys.argv.(1) in
87
 
      List.iter (fun n -> print_int n; print_string " ") (sieve n);
88
 
      print_newline ()
89
 
    with Failure "int_of_string" ->
90
 
      print_string "Bad integer constant";
91
 
      print_newline ()
92
 
  end
93
 
;;
94
 
 
95
 
if !Sys.interactive then () else main ();;