-
Notifications
You must be signed in to change notification settings - Fork 0
/
2010-04-a.pl
57 lines (31 loc) · 981 Bytes
/
2010-04-a.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
% Unofficial solutions to 2010-04 programming exam
% Everything works for the given test cases
% 1
differences([], []).
differences([_], []).
differences([A, B|T], [R|M]) :- R is B - A, differences([B|T], M).
% 2
evens([], []).
evens([_], []).
evens([_,B|T], [B|S]) :- evens(T, S).
% 3
atoms(T, L) :- setof(X, literal(X, T), L).
literal(X, X) :- atom(X).
literal(X, T) :- member(X, T).
literal(X, T) :- functor(T, X, _).
literal(X, T) :- compound(T), functor(T, _, N), N > 0, literal(X, T, 1).
literal(X, T, N) :- compound(T), arg(N, T, A), literal(X, A).
literal(X, T, N) :- functor(T, _, K), N > 0, N < K, M is N + 1, literal(X, T, M).
% 4
r(a, b).
r(b, c).
r(c, d).
r(d, a).
r(d, e).
r(d, f).
r(f, a).
cycle([H|T]) :- cycle(H, [H|T], []).
cycle(S, [E, S], _) :- r(E, S).
cycle(S, [A, B|T], V) :- \+ member(A, V), r(A, B), cycle(S, [B|T], [A|V]).
% With this solution cycle(X) also works for non-ground X,
% which seems to come up in other past papers.