Last active
June 15, 2019 23:45
-
-
Save awentzonline/f42eecdbb28fd21a60fb76381c301579 to your computer and use it in GitHub Desktop.
Find a family tree which contains an Uncle-Brother relation
This file contains hidden or 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
| male(donnie). | |
| male(jimmy). | |
| male(jethro). | |
| male(cletus). | |
| female(krystal). | |
| female(ruthie). | |
| female(blanche). | |
| female(ruby). | |
| person(A) :- | |
| male(A); female(A). | |
| parent_of(Parent, Child, ParentOf) :- | |
| person(Parent), person(Child), | |
| Parent \== Child, | |
| member(Parent-Child, ParentOf). | |
| has_parents(A, ParentOf) :- | |
| male(Dad), female(Mom), | |
| A \== Mom, A \== Dad, | |
| member(Mom-A, ParentOf), | |
| member(Dad-A, ParentOf). | |
| same_mom(A, B, ParentOf) :- | |
| female(Mom), | |
| parent_of(Mom, A, ParentOf), | |
| parent_of(Mom, B, ParentOf). | |
| same_dad(A, B, ParentOf) :- | |
| male(Dad), | |
| parent_of(Dad, A, ParentOf), | |
| parent_of(Dad, B, ParentOf). | |
| brothers(A, B, ParentOf) :- | |
| male(A), male(B), | |
| A \== B, | |
| has_parents(A, ParentOf), | |
| has_parents(B, ParentOf), | |
| (same_dad(A, B, ParentOf); % They share either a mom or dad | |
| same_mom(A, B, ParentOf)). % No solutions for same mom and dad | |
| uncle_of(Uncle, Nephew, ParentOf) :- | |
| male(Uncle), person(Nephew), | |
| Uncle \== Nephew, | |
| brothers(Uncle, Brother, ParentOf), | |
| parent_of(Brother, Nephew, ParentOf). | |
| uncle_brother_of(UncleBrother, Pivot, ParentOf) :- | |
| uncle_of(UncleBrother, Pivot, ParentOf), | |
| brothers(UncleBrother, Pivot, ParentOf). | |
| uncle_brother(UB, Pivot, ParentOf) :- | |
| length(ParentOf, _), | |
| uncle_brother_of(UB, Pivot, ParentOf). | |
| :- set_prolog_flag(verbose, silent). | |
| :- initialization(main). | |
| main :- | |
| UncleBrother = jethro, | |
| uncle_brother(UncleBrother, Pivot, FamilyTree), | |
| writef('Uncle brother of %w is %w\n', [Pivot, UncleBrother]), | |
| writef('Family tree: %w\n', [FamilyTree]), | |
| halt. | |
| main :- | |
| writef('No solution\n'), | |
| halt. |
Author
I haven't written prolog since college so true 'loggers might hate this.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.