Instantly share code, notes, and snippets.

# Pagliacii/father-puzzle.pl

Created June 2, 2021 09:17
Who is Lorna’s father? Use Prolog to solve a logic puzzle.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 /* 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 commented Jun 2, 2021 • edited

```\$ 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