Courses/CS 332L/Spring 2006/Robert Lai/Homework 1

From CSWiki

Jump to: navigation, search
%Name:	Robert Lai
%CIN:	219022282
%File Name: Homework_1.pl

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%1.2

male(albert).
male(edward).

female(alice).
female(victoria).

parents(edward, victoria, albert).
parents(alice, victoria, albert).

sister_of(X, Y) :- female(X), parents(X, M, F), parents(Y, M, F).

sister_of_2(X, Y) :- female(X), parents(X, M, F), parents(Y, M, F), X\=Y.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%The answer is
%
%X = alice
%Y = edward ;
%
%X = alice
%Y = alice ;
%
%No
%
%The reason it has two answer is because when the program first rum it check X for female first
%and then it will get alice and victoria. After that it will check for the parents to see if daughter of the other two peoples
% and it get X is alice and search for Y. Y can either be male of female. Since albert and victoria are parents of edward and alice,
% then it will get edward. when it search for alice, alice is female and her parents are albert and victoria, (the same M, and F),
% therefore, the it print out the second answer X=alice, and Y=alice.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%1.3

father(X, Y) :- male(X), parents(Y, _, X).
mother(X, Y) :- female(X), parents(Y, X, _).
is_male(X) :- male(X).
is_female(X) :- female(X).
parent(X, Y) :- father(X, Y); mother(X, Y).
diff(X, Y) :- X\=Y.

is_mother(X) :- mother(X, _).
is_father(X) :- father(X, _).
is_son(X) :- male(X), parent(_, X).
grandpa_of(X, Y) :- male(X), father(X, Z), father(Z, Y).
sibling(X, Y) :- parent(Z, X), parent(Z, Y), diff(X, Y).
aunt(X, Y) :- sister_of(X, Z), parent(Z, Y).
Personal tools