Hurry omg.
This commit is contained in:
parent
29b2d280b9
commit
40837bf9d7
4 changed files with 243 additions and 17 deletions
35
hw3/README.txt
Normal file
35
hw3/README.txt
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
//Alexander Huddleston
|
||||||
|
//CSCE 420
|
||||||
|
//Due: November 15, 2017
|
||||||
|
//README.txt
|
||||||
|
|
||||||
|
run gprolog
|
||||||
|
[hw3prX].
|
||||||
|
|
||||||
|
problem 1 expects arguments:
|
||||||
|
|
||||||
|
my_sort(Unsorted, Sorted).
|
||||||
|
|
||||||
|
problem 2 expects arguments:
|
||||||
|
|
||||||
|
my_topo_sort(Partial_order, Total_order).
|
||||||
|
|
||||||
|
problem 3 can tell you these relations:
|
||||||
|
|
||||||
|
is_spouse(X, Y). NOTE: Only expected to be used in the rest of the definitions.
|
||||||
|
is_child(X, Y). NOTE: Only expected to be used in the rest of the definitions.
|
||||||
|
|
||||||
|
grandchild(X, Y).
|
||||||
|
greatgrandparent(X, Y).
|
||||||
|
ancestor(X, Y).
|
||||||
|
brother(X, Y).
|
||||||
|
sister(X, Y).
|
||||||
|
daughter(X, Y).
|
||||||
|
son(X, Y).
|
||||||
|
first_cousin(X, Y).
|
||||||
|
brother_in_law(X, Y).
|
||||||
|
sister_in_law(X, Y).
|
||||||
|
aunt(X, Y).
|
||||||
|
uncle(X, Y).
|
||||||
|
|
||||||
|
cousin(X, Y). NOTE: This just called first_cousin(X, Y), since a cousin is a synonym for first cousin.
|
|
@ -1,12 +1,48 @@
|
||||||
my_sort([], Sorted2, Sorted2).
|
:- dynamic(greater/2).
|
||||||
|
|
||||||
my_sort(Unsorted, Sorted, Sorted2) :-
|
make_rules([]).
|
||||||
|
|
||||||
|
make_rules([X]).
|
||||||
|
|
||||||
|
make_rules([X,Y|XS]) :-
|
||||||
|
\+ is_greater(X, Y),
|
||||||
|
assertz(greater(Y, X)),
|
||||||
|
append([Y], XS, XS2),
|
||||||
|
make_rules(XS2).
|
||||||
|
|
||||||
|
all_rules([]).
|
||||||
|
|
||||||
|
all_rules(Partial_order) :-
|
||||||
|
select(X, Partial_order, Partial_order2),
|
||||||
|
make_rules(X),
|
||||||
|
all_rules(Partial_order2).
|
||||||
|
|
||||||
|
is_greater(Y, X) :-
|
||||||
|
greater(Y, X);
|
||||||
|
greater(Z, X),
|
||||||
|
is_greater(Y, Z).
|
||||||
|
|
||||||
|
remove_duplicates([], Unsorted_flat_concise, Unsorted_flat_concise).
|
||||||
|
|
||||||
|
remove_duplicates(Unsorted_flat, Unsorted_flat2, Unsorted_flat_concise) :-
|
||||||
|
select(X, Unsorted_flat, Unsorted_flat_rest),
|
||||||
|
(member(X, Unsorted_flat2),
|
||||||
|
remove_duplicates(Unsorted_flat_rest, Unsorted_flat2, Unsorted_flat_concise);
|
||||||
|
append([X], Unsorted_flat2, Unsorted_flat3),
|
||||||
|
remove_duplicates(Unsorted_flat_rest, Unsorted_flat3, Unsorted_flat_concise)).
|
||||||
|
|
||||||
|
my_topo_sort([], Sorted2, Sorted2).
|
||||||
|
|
||||||
|
my_topo_sort(Unsorted, Sorted, Sorted2) :-
|
||||||
get_max(Unsorted, Max, LessList),
|
get_max(Unsorted, Max, LessList),
|
||||||
append(Sorted, [Max], NewSorted),
|
append(Sorted, [Max], NewSorted),
|
||||||
my_sort(LessList, NewSorted, Sorted2).
|
my_topo_sort(LessList, NewSorted, Sorted2).
|
||||||
|
|
||||||
my_sort(Unsorted, Sorted) :-
|
my_topo_sort(Unsorted, Sorted) :-
|
||||||
my_sort(Unsorted, [], ReverseSorted),
|
all_rules(Unsorted),
|
||||||
|
flatten(Unsorted, Unsorted_flat),
|
||||||
|
remove_duplicates(Unsorted_flat, [], Unsorted_flat_concise),
|
||||||
|
my_topo_sort(Unsorted_flat_concise, [], ReverseSorted),
|
||||||
reverse(ReverseSorted, Sorted),
|
reverse(ReverseSorted, Sorted),
|
||||||
write(Sorted).
|
write(Sorted).
|
||||||
|
|
||||||
|
@ -14,7 +50,7 @@ get_max([], Max, LessList, Max, LessList).
|
||||||
|
|
||||||
get_max(Unsorted, X, Unsorted2, Max, LessList) :-
|
get_max(Unsorted, X, Unsorted2, Max, LessList) :-
|
||||||
select(Y, Unsorted, Rest),
|
select(Y, Unsorted, Rest),
|
||||||
(X > Y,
|
(is_greater(X, Y),
|
||||||
append(Unsorted2, [Y], NewList),
|
append(Unsorted2, [Y], NewList),
|
||||||
get_max(Rest, X, NewList, Max, LessList);
|
get_max(Rest, X, NewList, Max, LessList);
|
||||||
append(Unsorted2, [X], NewList),
|
append(Unsorted2, [X], NewList),
|
||||||
|
|
133
hw3/hw3pr3.pl
Normal file
133
hw3/hw3pr3.pl
Normal file
|
@ -0,0 +1,133 @@
|
||||||
|
male(george).
|
||||||
|
male(phillip).
|
||||||
|
male(spencer).
|
||||||
|
male(charles).
|
||||||
|
male(mark).
|
||||||
|
male(andrew).
|
||||||
|
male(edward).
|
||||||
|
male(william).
|
||||||
|
male(harry).
|
||||||
|
male(peter).
|
||||||
|
male(mike).
|
||||||
|
male(james).
|
||||||
|
male(george2).
|
||||||
|
|
||||||
|
spouse(george, mum).
|
||||||
|
spouse(elizabeth, philip).
|
||||||
|
spouse(spencer, kydd).
|
||||||
|
spouse(diana, charles).
|
||||||
|
spouse(anne, mark).
|
||||||
|
spouse(andrew, sarah).
|
||||||
|
spouse(edward, sophie).
|
||||||
|
spouse(william, catherine).
|
||||||
|
spouse(peter, autumn).
|
||||||
|
spouse(zara, mike).
|
||||||
|
|
||||||
|
child(elizabeth, mum).
|
||||||
|
child(margaret, mum).
|
||||||
|
child(charles, elizabeth).
|
||||||
|
child(anne, elizabeth).
|
||||||
|
child(andrew, elizabeth).
|
||||||
|
child(edward, elizabeth).
|
||||||
|
child(diana, kydd).
|
||||||
|
child(william, diana).
|
||||||
|
child(harry, diana).
|
||||||
|
child(peter, anne).
|
||||||
|
child(zara, anne).
|
||||||
|
child(beatrice, sarah).
|
||||||
|
child(eugenie, sarah).
|
||||||
|
child(louise, sophie).
|
||||||
|
child(james, sophie).
|
||||||
|
child(george2, catherine).
|
||||||
|
child(charlotte, catherine).
|
||||||
|
child(savannah, autumn).
|
||||||
|
child(isla, autumn).
|
||||||
|
child(mia, zara).
|
||||||
|
|
||||||
|
is_spouse(X, Y) :-
|
||||||
|
spouse(X,Y);
|
||||||
|
spouse(Y,X).
|
||||||
|
|
||||||
|
is_child(X, Y) :-
|
||||||
|
child(X, Y);
|
||||||
|
(is_spouse(Z, Y),
|
||||||
|
child(X, Z)).
|
||||||
|
|
||||||
|
grandchild(X, Y) :-
|
||||||
|
is_child(Z, Y),
|
||||||
|
is_child(X, Z).
|
||||||
|
|
||||||
|
greatgrandparent(X, Y) :-
|
||||||
|
(is_child(Z, X),
|
||||||
|
is_child(A, Z),
|
||||||
|
is_child(Y, A));
|
||||||
|
(is_spouse(X,B),
|
||||||
|
is_child(Z, B),
|
||||||
|
is_child(A, Z),
|
||||||
|
is_child(Y, A)).
|
||||||
|
|
||||||
|
ancestor(X, Y) :-
|
||||||
|
is_child(Y, X);
|
||||||
|
(is_child(Z, X),
|
||||||
|
ancestor(Z, Y)).
|
||||||
|
|
||||||
|
brother(X, Y) :-
|
||||||
|
child(X, A),
|
||||||
|
child(Y, A),
|
||||||
|
male(X),
|
||||||
|
\==(X, Y).
|
||||||
|
|
||||||
|
sister(X, Y) :-
|
||||||
|
child(X, A),
|
||||||
|
child(Y, A),
|
||||||
|
\+ male(X),
|
||||||
|
\==(X, Y).
|
||||||
|
|
||||||
|
daughter(X, Y) :-
|
||||||
|
is_child(X, Y),
|
||||||
|
\+ male(X).
|
||||||
|
|
||||||
|
son(X, Y) :-
|
||||||
|
is_child(X, Y),
|
||||||
|
male(X).
|
||||||
|
|
||||||
|
first_cousin(X, Y) :-
|
||||||
|
(uncle(A, X),
|
||||||
|
is_child(Y, A)).
|
||||||
|
|
||||||
|
brother_in_law(X, Y) :-
|
||||||
|
(spouse(B, X),
|
||||||
|
child(B, A),
|
||||||
|
child(Z, A),
|
||||||
|
spouse(Y, Z),
|
||||||
|
\+ is_spouse(Y, X),
|
||||||
|
male(X));
|
||||||
|
(child(X, A),
|
||||||
|
child(Z, A),
|
||||||
|
spouse(Y, Z),
|
||||||
|
\+ is_spouse(Y, X),
|
||||||
|
male(X)).
|
||||||
|
|
||||||
|
sister_in_law(X, Y) :-
|
||||||
|
(spouse(B, X),
|
||||||
|
child(B, A),
|
||||||
|
child(Z, A),
|
||||||
|
spouse(Y, Z),
|
||||||
|
\+ is_spouse(Y, X),
|
||||||
|
\+ male(X));
|
||||||
|
(child(X, A),
|
||||||
|
child(Z, A),
|
||||||
|
spouse(Y, Z),
|
||||||
|
\+ is_spouse(Y, X),
|
||||||
|
\+ male(X)).
|
||||||
|
|
||||||
|
aunt(X, Y) :-
|
||||||
|
is_child(Y, Z),
|
||||||
|
sister(X, Z).
|
||||||
|
|
||||||
|
uncle(X, Y) :-
|
||||||
|
is_child(Y, Z),
|
||||||
|
brother(X, Z).
|
||||||
|
|
||||||
|
cousin(X, Y) :-
|
||||||
|
first_cousin(X, Y).
|
22
hw3/testing.pl
Normal file
22
hw3/testing.pl
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
:- dynamic(greater/2).
|
||||||
|
|
||||||
|
make_rules([]).
|
||||||
|
|
||||||
|
make_rules([X]).
|
||||||
|
|
||||||
|
make_rules([X,Y|XS]) :-
|
||||||
|
assertz(greater(Y, X)),
|
||||||
|
append([Y], XS, XS2),
|
||||||
|
make_rules(XS2).
|
||||||
|
|
||||||
|
my_topo_sort([], Total_order).
|
||||||
|
|
||||||
|
my_topo_sort(Partial_order, Total_order) :-
|
||||||
|
select(X, Partial_order, Partial_order2),
|
||||||
|
make_rules(X),
|
||||||
|
my_topo_sort(Partial_order2, Total_order).
|
||||||
|
|
||||||
|
is_greater(Y, X) :-
|
||||||
|
greater(Y, X);
|
||||||
|
greater(Z, X),
|
||||||
|
is_greater(Y, Z).
|
Reference in a new issue