Skip to content

Instantly share code, notes, and snippets.

@nelsonni
Last active August 29, 2015 14:22
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 nelsonni/d4657e4b5c3da7e84061 to your computer and use it in GitHub Desktop.
Save nelsonni/d4657e4b5c3da7e84061 to your computer and use it in GitHub Desktop.
HW5.pl
% Nicholas Nelson (931-242-585)
%
% CS 381: Homework 5
% 6.2.2015
%
% Here are a bunch of facts describing the Simpson's family tree.
% Don't change them!
female(mona).
female(jackie).
female(marge).
female(patty).
female(selma).
female(lisa).
female(maggie).
female(ling).
male(abe).
male(clancy).
male(herb).
male(homer).
male(bart).
married_(abe,mona).
married_(clancy,jackie).
married_(homer,marge).
married(X,Y) :- married_(X,Y).
married(X,Y) :- married_(Y,X).
parent(abe,herb).
parent(abe,homer).
parent(mona,homer).
parent(clancy,marge).
parent(jackie,marge).
parent(clancy,patty).
parent(jackie,patty).
parent(clancy,selma).
parent(jackie,selma).
parent(homer,bart).
parent(marge,bart).
parent(homer,lisa).
parent(marge,lisa).
parent(homer,maggie).
parent(marge,maggie).
parent(selma,ling).
%%
% Part 1. Family relations
%%
% 1. Define a predicate `child/2` that inverts the parent relationship.
child(A,B) :- parent(B,A).
% 2. Define two predicates `isMother/1` and `isFather/1`.
isMother(A) :- parent(A,_), female(A).
isFather(A) :- parent(A,_), male(A).
% 3. Define a predicate `grandparent/2`.
grandparent(A,B) :- parent(A,X), parent(X,B).
% 4. Define a predicate `sibling/2`. Siblings share at least one parent.
sibling(A,B) :- parent(X,A), parent(X,B), A \= B.
% 5. Define two predicates `brother/2` and `sister/2`.
brother(A,B) :- sibling(A,B), male(A).
sister(A,B) :- sibling(A,B), female(A).
% 6. Define a predicate `siblingInLaw/2`. A sibling-in-law is either married to
% a sibling or the sibling of a spouse.
siblingInLaw(A,B) :- married(A,X), sibling(X,B).
siblingInLaw(A,B) :- sibling(A,X), married(B,X).
% 7. Define two predicates `aunt/2` and `uncle/2`. Your definitions of these
% predicates should include aunts and uncles by marriage.
aunt(A,B) :- sibling(A,X), parent(X,B), female(A).
aunt(A,B) :- siblingInLaw(A,X), parent(X,B), female(A).
uncle(A,B) :- sibling(A,X), parent(X,B), male(A).
uncle(A,B) :- siblingInLaw(A,X), parent(X,B), male(A).
% 8. Define the predicate `cousin/2`.
cousin(A,B) :- sibling(X,Y), parent(X,A), parent(Y,B).
% 9. Define the predicate `ancestor/2`.
ancestor(A,B) :- parent(A,B).
ancestor(A,B) :- parent(A,X), ancestor(X,B).
% Extra credit: Define the predicate `related/2`.
descendent(A,B) :- ancestor(B,A).
relatedP(A,B) :- married(A,B).
relatedP(A,B) :- ancestor(A,B).
relatedP(A,B) :- descendent(A,B).
related(A,B) :- relatedP(A,B), A \= B.
related(A,B) :- relatedP(A,X), relatedP(X,B), A \= B.
%%
% Part 2. List manipulation
%%
% 10. Define a predicate `rdup(L,M)` that removes adjacent duplicate elements
% from the list `L`. The resulting list should be bound to `M`. It's OK if
% this function loops on queries where `L` is not provided.
rdup([],[]).
rdup([L],[L]).
rdup([L,L|R],R2) :- rdup([L|R],R2).
rdup([L,X|R],[L|R2]) :- L \= X, rdup([X|R],R2).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment