Subversion Repositories tendra.SVN

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
6 7u83 1
/*
2
    		 Crown Copyright (c) 1997
3
 
4
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
12
 
13
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
15
 
16
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
19
 
20
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
22
        these conditions;
23
 
24
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
27
        it may be put.
28
*/
29
 
30
 
31
/*
32
**  primes2.pl:
33
**
34
**    Same as primes.pl but with suitable general procedures.
35
*/
36
 
37
 
38
/* Define a recursive data structure */
39
 
40
Struct Chain (a:Int, b:pointer(Chain_Align));
41
Al_tagdef Chain_Align = alignment (Chain);
42
 
43
Iddec printf : proc;
44
Iddec falls_through_sieve : proc;
45
Iddec apply_sieve : proc;
46
 
47
Var count_down : Int;
48
Var next_integer : Int;
49
Var global_chain : pointer(Chain_Align);
50
 
51
 
52
String first = "The first %d primes:\n\n";
53
String s = "%d\n";
54
 
55
 
56
Proc falls_through_sieve = General Int (; z:pointer(Chain_Align) ... ) Untidy
57
{
58
    ? { *? ((* z) == make_null_ptr(Chain_Align));
59
	untidy_return (0(Int))
60
      |
61
	make_top
62
    };
63
 
64
    Let a_z = a [(*(Chain) (* z))]
65
    {
66
	? { ? ((a_z * a_z) > (* next_integer));
67
	    return (0(Int))
68
	  |
69
	    make_top
70
	};
71
 
72
	? { ? (((* next_integer) % a_z) == 0(Int));
73
	    untidy_return (1(Int))
74
	  |
75
	    z = b [(*(Chain) (* z))];
76
	    falls_through_sieve Tail_call [Same ... ]
77
	}
78
    }
79
};
80
 
81
 
82
Proc apply_sieve = General top (; z:pointer(alignment(pointer(Chain_Align))) ... ) Untidy
83
Var link : Chain
84
{
85
    ? { ? ((* count_down) < 1(Int));
86
	return (make_top)
87
      |
88
	make_top
89
    };
90
 
91
    ? { ? (falls_through_sieve [Int] [; (* global_chain) ... ] != 0(Int));
92
	next_integer = ((* next_integer) + 1(Int));
93
	apply_sieve Tail_call [Same ... ];
94
      |
95
	(link *+. .a) = (* next_integer);
96
	(link *+. .b) = make_null_ptr (Chain_Align);
97
	(* z) = link;
98
 
99
	printf [top] (s, (* next_integer));
100
 
101
	next_integer = ((* next_integer) + 1(Int));
102
	count_down = ((* count_down) - 1(Int));
103
	z = (link *+. .b);
104
	apply_sieve [top] [; Same ... ];	/* Cannot be a tail call */
105
    };
106
 
107
    untidy_return (make_top)
108
};
109
 
110
 
111
Proc sieve = top (n:Int)
112
{
113
    printf [top] (first, (* n));
114
    count_down = (* n);
115
    next_integer = 2(Int);
116
    global_chain = make_null_ptr (Chain_Align);
117
    apply_sieve Tail_call [global_chain ... ];
118
};
119
 
120
 
121
Proc main = top ()
122
{
123
    sieve [top] (25(Int));
124
 
125
    return (make_top)
126
};
127
 
128
Keep(main, sieve, apply_sieve, falls_through_sieve,
129
     first, s, global_chain, next_integer, count_down,
130
     Chain, Chain_Align)