10
11?- dynamic been_here/0. 12
15
18
19memberd(A, L) :- once(member(A,L)).
20
23
24my_random(R, N) :-
25 26 random_between(1, R, N),
27 !.
28
31
32probability2(P, M) :-
33 random(X),
34 Y is X*M,
35 Y =< P,
36 !.
37
38size_of([], 0).
39size_of([_|R], K) :-
40 size_of(R, L),
41 K is L + 1,
42 !.
43
45
46writel([]) :- !, ttyflush.
47writel([A|R]) :- var(A), !, write(A), write(' '), writel(R).
48writel([nl|R]) :- !, nl, writel(R).
49writel([A|R]) :- !, write(A), write(' '), writel(R).
50writel(A) :- write(A), ttyflush, !.
51
52writel2([]) :- !, ttyflush.
53writel2([A|R]) :- var(A), !, write(A), writel2(R).
54writel2([nl|R]) :- !, nl, writel2(R).
55writel2([A|R]) :- !, write(A), writel2(R).
56writel2(A) :- write(A), ttyflush, !.
57
85
86copy_struct(S, T) :-
87 assert(temp(S)),
88 retract(temp(T)),
89 !.
90
91count(_, [], 0).
92count(X, [X|Y], C) :-
93 !,
94 count(X, Y, C2),
95 C is C2 + 1.
96count(X, [_|Y], C) :-
97 count(X, Y, C).
98
99round(X, Y) :- Y is integer(X+0.5), !.
100
101set_random_number_gen :-
102 been_here,
103 !.
104set_random_number_gen :-
105 assert(been_here),
106 !,
107 seed_P(X,Y),
108 set_seed(X,Y).
109
110set_seed(default, _) :- !.
111set_seed(random, _) :-
112 datime(datime(Year,Month,Day,Hour,Min,Sec)),
113 114 N is (((((Year-1970)*12+Month)*30+Day)*24+Hour)*60+Min)*60+Sec,
115 R1 is mod(N, 30270) + 1, 116 N2 is abs(N >> 2), 117 R2 is mod(N2, 30308) + 1,
118 N3 is abs(N >> 4), 119 R3 is mod(N3, 30324) + 1,
120 setrand(rand(R1,R2,R3)),
121 retract(seed_P(_,_)),
122 assert(seed_P(random, (R1,R2,R3))),
123 !.
124set_seed(manual, (X, Y, Z)) :-
125 setrand(rand(X,Y,Z)).
126
127
147
148debug_echo(L) :- debug_set_P(yes), !, writel(L).
149debug_echo(_).
150
151rem_dups([], []).
152rem_dups([A|R], R2) :-
153 member(A, R),
154 !,
155 rem_dups(R, R2).
156rem_dups([A|R], [A|R2]) :-
157 rem_dups(R, R2),
158 !.
159
160average(M, Avg) :-
161 sum_list(M, Sum),
162 size_of(M, N),
163 Avg is Sum / N,
164 !.
165
167
168extend_list(A, _, K, A) :-
169 length(A, K2),
170 K2 >= K,
171 !.
172extend_list(A, B, K, A2) :-
173 append(A, B, A3),
174 extend_list(A3, B, K, A2),
175 !.
176
177num_list(0, []) :- !.
178num_list(N, [N|R]) :-
179 M is N - 1,
180 num_list(M, R).
181
182remove(_, [], []).
183remove(A, [A|B], B).
184remove(A, [X|B], [X|C]) :- remove(A, B, C).
185
186remove_all(_, [], []).
187remove_all(A, [A|B], C) :- !, remove_all(A, B, C).
188remove_all(A, [X|B], [X|C]) :- remove_all(A, B, C).
189
190
191intersect([], _,[]).
192intersect([X|Y], R, [X|Z]) :-
193 member(X, R),
194 !,
195 intersect(Y, R, Z).
196intersect([_|Y], R, Z) :-
197 intersect(Y, R, Z).
198
199set_diff([], T, T) :- !.
200set_diff(T, [], T) :- !.
201set_diff([A|B], T, Diff) :-
202 member(A, T),
203 !,
204 remove_all(A, T, T2),
205 set_diff(B, T2, Diff).
206set_diff([A|B], T, [A|R]) :-
207 set_diff(B, T, R).
208
209remove_list([], B, B) :- !.
210remove_list([A|B], C, D) :-
211 remove(A, C, E),
212 !,
213 remove_list(B, E, D).
214
215writelist([]) :- nl, !.
216writelist([A|R]) :- write(A), nl, writelist(R), !.
217
218maybe :- maybe(0.5), !.
219
220maybe(X) :-
221 random(Y),
222 Y < X,
223 !.
224
225random_permutation(L, Perm) :-
226 length(L, Len),
227 random_permutation2(L, Len, Perm),
228 !.
229
230random_permutation2([], _, []).
231random_permutation2(L, Len, [X|Perm]) :-
232 random(0, Len, R),
233 remove_nth(R, L, X, L2),
234 Len2 is Len-1,
235 random_permutation2(L2, Len2, Perm),
236 !.
237
238remove_nth(0, [X|Y], X, Y) :- !.
239remove_nth(N, [X|Y], Z, [X|W]) :-
240 N2 is N-1,
241 remove_nth(N2, Y, Z, W),
242 !.
243
244select_rand(L, R) :-
245 length(L, Len),
246 Len > 0,
247 random(0, Len, Rand),
248 remove_nth(Rand, L, R, _),
249 !.
250
251
254
255first_K(M, N, _, []) :- M >= N, !.
256first_K(M, N, [A|R], [A|S]) :-
257 M2 is M + 1,
258 first_K(M2, N, R, S),
259 !