Skip to content

Instantly share code, notes, and snippets.

@superRaytin
Last active July 4, 2019 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save superRaytin/0e920db87f0d4068f56822f2141b6266 to your computer and use it in GitHub Desktop.
Save superRaytin/0e920db87f0d4068f56822f2141b6266 to your computer and use it in GitHub Desktop.
A Prolog program to find people's lover
% A Prolog program to find people's lover
% --------------------------------
man(george). man(jack). man(tony).
woman(lucy). woman(fiona). woman(peggy).
% % A man can only fall in love with one woman
% % Women are already in different rooms and they like to eat different foods
uniq(A, B, C) :- woman(A), woman(B), woman(C), \+A=B, \+A=C, \+B=C.
% % Clue 1:Room1 has bananas, Peggy isn't in Room1
clue1(GL, JL, TL, R1, R2, R3, Apple, Orange, Banana) :-
R1=Banana, \+R1=peggy.
% % Clue 2:Tony's lover likes orange,Lucy doesn't like Tony
clue2(GL, JL, TL, R1, R2, R3, Apple, Orange, Banana) :-
Orange=TL, \+TL=lucy.
% % Clue 3:Fiono doesn't like bananas
clue3(GL, JL, TL, R1, R2, R3, Apple, Orange, Banana) :-
Banana=fiona.
% % Clue 4:Jack's lover is in Room2 and she likes apple
clue4(GL, JL, TL, R1, R2, R3, Apple, Orange, Banana) :-
JL=R2, JL=Apple.
% % Clue 5:Room3 has oranges
clue5(GL, JL, TL, R1, R2, R3, Apple, Orange, Banana) :-
R3=Orange.
% % Show these people’s lover
showLovers(GL, JL, TL, R1, R2, R3, Apple, Orange, Banana) :-
write("George’s lover is: "), write(GL), nl,
write("Jack’s lover is: "), write(JL), nl,
write("Tony’s lover is: "), write(TL), nl,
write(R1), write(" in Room1"), nl,
write(R2), write(" in Room2"), nl,
write(R3), write(" in Room3"), nl,
write(Apple), write(" likes apple"), nl,
write(Orange), write(" likes orange"), nl,
write(Banana), write(" likes banana"), nl.
% % Here we go
findLovers(GL, JL, TL) :-
uniq(R1, R2, R3),
uniq(Apple, Orange, Banana),
uniq(GL, JL, TL),
clue1(GL, JL, TL, R1, R2, R3, Apple, Orange, Banana),
clue2(GL, JL, TL, R1, R2, R3, Apple, Orange, Banana),
clue3(GL, JL, TL, R1, R2, R3, Apple, Orange, Banana),
clue4(GL, JL, TL, R1, R2, R3, Apple, Orange, Banana),
clue5(GL, JL, TL, R1, R2, R3, Apple, Orange, Banana),
showLovers(GL, JL, TL, R1, R2, R3, Apple, Orange, Banana).
% % Answers:
% % George loves Fiona(in Room1, she likes banana)
% % Jack loves Lucy(in Room2, she likes apple)
% % Tony loves Peggy(in Room3, she likes orange)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment