Skip to content

Instantly share code, notes, and snippets.

@Pagliacii
Created June 2, 2021 09:17
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 Pagliacii/faf06a48ba373bfe376d293eaed66175 to your computer and use it in GitHub Desktop.
Save Pagliacii/faf06a48ba373bfe376d293eaed66175 to your computer and use it in GitHub Desktop.
Who is Lorna’s father? Use Prolog to solve a logic puzzle.
/* Who is Lorna's father?
*
* Mary Ann Moore’s father has a yacht and so has each of his four friends: Colonel Downing, Mr. Hall, Sir Barnacle Hood, and Dr. Parker.
* 1. Each of the five also has one daughter and each has named his yacht after a daughter of one of the others.
* 2. Sir Barnacle’s yacht is the Gabrielle.
* 3. Mr. Moore owns the Lorna.
* 4. Mr. Hall the Rosalind.
* 5. The Melissa, owned by Colonel Downing, is named after Sir Barnacle’s daughter.
* 6. Gabrielle’s father owns the yacht that is named after Dr. Parker’s daughter.
*/
lorna_father(Father) :-
whos_father(lorna, Father).
whos_father(Daughter, Father) :-
fathers(Fs),
member(f(Father, Daughter, _), Fs).
fathers(Fs) :-
length(Fs, 5), % 1
member(f(hood, _, gabrielle), Fs), % 2
member(f(moore, _, lorna), Fs), % 3
member(f(hall, _, rosalind), Fs), % 4
member(f(downing, _, melissa), Fs), % 5
member(f(hood, melissa, _), Fs), % 5
member(f(_, gabrielle, D), Fs), % 6
member(f(parker, D, _), Fs), % 6
member(f(moore, mary, _), Fs),
member(f(_, _, mary), Fs),
member(f(_, lorna, _), Fs),
member(f(_, rosalind, _), Fs),
nth0(0, Fs, f(hood, _, _)),
nth0(1, Fs, f(moore, _, _)),
nth0(2, Fs, f(downing, _, _)),
nth0(3, Fs, f(hall, _, _)),
nth0(4, Fs, f(parker, _, _)).
@Pagliacii
Copy link
Author

Pagliacii commented Jun 2, 2021

Answer
$ swipl father-puzzle.pl

?- lorna_father(Father).
Father = downing ;
false.

?- fathers(Fs).
Fs = [f(hood, melissa, gabrielle), f(moore, mary, lorna), f(downing, lorna, melissa), f(hall, gabrielle, rosalind), f(parker, rosalind, mary)] ;
false.
Father Daughter Yacht
Sir Barnacle Hood Melissa Gabrielle
Mr. Moore Mary Ann Lorna
Colonel Downing Lorna Melissa
Mr. Hall Gabrielle Rosalind
Dr. Parker Rosalind Mary

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment