-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathChapter 19.pl
More file actions
155 lines (133 loc) · 4.33 KB
/
Copy pathChapter 19.pl
File metadata and controls
155 lines (133 loc) · 4.33 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
% Exercise 1
% Define a mother predicate so that mother(X,Y) says that X is the mother of Y.
mother(X, Y) :-
female(X),
parent(X, Y).
% Exercise 2
% Define a father predicate so that father(X,Y) says that X is the father of Y.
father(X, Y) :-
male(X),
parent(X, Y).
% Exercise 3
% Define a sister predicate so that sister(X,Y) says that X is a sister of Y.
sister(X, Y) :-
X \= Y,
female(X),
parent(Z, X),
parent(Z, Y).
% Exercise 4
% Define a grandson predicate so that grandson(X,Y) says that X is a grandson of Y.
grandson(X, Y) :-
male(X),
parent(Z, X),
parent(Y, Z).
% Exercise 5
% Define the firstCousin predicate so that firstCousin(X,Y) says that X is a first cousin of Y.
firstCousin(X, Y) :-
X \= Y,
parent(P1, X),
parent(P2, Y),
P1 \= P2,
parent(Z, P1),
parent(Z, P2).
% Exercise 6
% Define the descendant predicate so that descendant(X,Y) says that X is a descendant of Y.
descendant(X, Y) :-
parent(Y, X).
descendant(X, Y) :-
parent(Z, X),
descendant(Z, Y).
% Exercise 7
% Define a third predicate so that third(X,Y) says that Y is the third element of the list X.
third([_, _, X|_], X).
% Exercise 8
% Define a firstPair predicate so that firstPair(X) succeeds if and only if X is a list of at least two elements, with the first element the same as the second element.
firstPair([X, X|_]).
% Exercise 9
% Define a del3 predicate so that del3(X,Y) says that the list Y is the same as the list X but with the third element deleted.
del3([A, B, C|T], [A, B|T]).
% Exercise 10
% Define a dupList predicate so that dupList(X,Y) says that X is a list and Y is the same list, but with each element of X repeated twice in a row.
dupList([], []).
dupList([H|Tx], [H, H|Ty]) :-
dupList(Tx, Ty).
% Exercise 11
% Define a predicate isDuped so that isDuped(Y) succeeds if and only if Y is a list of the form of the lists Y in Exercise 10.
isDuped([]).
isDuped([X, X|T]) :-
isDuped(T).
% Exercise 12
% Define the oddSize predicate so that oddSize(X) says that X is a list whose length is an odd number.
oddSize([_]).
oddSize([_, _|T]) :-
oddSize(T).
% Exercise 13
% Define the evenSize predicate so that evenSize(X) says that X is a list whose length is an even number.
evenSize([]).
evenSize([_, _|T]) :-
evenSize(T).
% Exercise 14
% Define the prefix predicate so that prefix(X,Y) says that X is a list that is a prefix of Y.
prefix([], _).
prefix([H|Tx], [H|Ty]) :-
prefix(Tx, Ty).
% Exercise 15
% Define the isMember predicate so that isMember(X,Y) says that element X is a member of set Y.
isMember(X, [X|_]).
isMember(X, [Y|T]) :-
X \= Y,
isMember(X, T).
% Exercise 16
% Define the isUnion predicate so that isUnion(X,Y,Z) says that the union of X and Y is Z.
isUnion([], [], []).
isUnion([], Y, Y).
isUnion([H|Tx], Y, [H|Tz]) :-
isUnion(Tx, Y, Tz),
\+ isMember(H, Y).
isUnion([H|Tx], Y, Z) :-
isUnion(Tx, Y, Z),
isMember(H, Y).
% Exercise 17
% Define the isIntersection predicate so that isIntersection(X,Y,Z) says that the intersection of X and Y is Z.
isIntersection([], _, []).
isIntersection([H|Tx], Y, [H|Tz]) :-
isIntersection(Tx, Y, Tz),
isMember(H, Y).
isIntersection([H|Tx], Y, Z) :-
isIntersection(Tx, Y, Z),
\+ isMember(H, Y).
% Exercise 18
% Define the isEqual predicate so that isEqual(X,Y) says that the sets X and Y are equal.
isEqual([], _).
isEqual([H|T], Y) :-
isMember(H, Y),
isEqual(T, Y).
% Exercise 19
% Define the powerset predicate so that powerset(X,Y) says that the powerset of X is Y.
powersetHelper([], []).
powersetHelper([H|Tl], [H|Tp]) :-
powersetHelper(Tl, Tp).
powersetHelper([_|Tl], P) :-
powersetHelper(Tl, P).
powerset(L, P) :-
findall(X, powersetHelper(L, X), P).
% Exercise 20
% Define the isDifference predicate so that isDifference(X,Y,Z) says that the set Z contains the elements of X that do not also appear in Y. Make a relation that works no matter what order Z is in.
diffHelper1(_, _, []).
diffHelper1(X, Y, [H|T]) :-
isMember(H, X),
\+ isMember(H, Y),
diffHelper1(X, Y, T).
diffHelper2([], _, []).
diffHelper2([H|T1], Y, [H|T2]) :-
\+ isMember(H, Y),
diffHelper2(T1, Y, T2).
diffHelper2([H|T], Y, Z) :-
isMember(H, Y),
diffHelper2(T, Y, Z).
isDifference(X, Y, Z) :-
\+ var(Z),
diffHelper1(X, Y, Z).
isDifference(X, Y, Z) :-
var(Z),
diffHelper2(X, Y, Z).