Prolog Sheet
Prolog Sheet
Define facts and rules to represent family relations. Write queries to find siblings, parents, etc.
% Facts
parent(john, jim).
parent(john, ann).
parent(ann, mary).
parent(jim, peter).
% Rules
sibling(X, Y) :- parent(Z, X), parent(Z, Y), X \= Y.
mother(X, Y) :- parent(X, Y), female(X).
father(X, Y) :- parent(X, Y), male(X).
% Queries
% ?- sibling(ann, jim). % Should return true
% ?- mother(john, mary). % Should return true
% ?- father(jim, peter). % Should return true
% Queries
% ?- list_length([1, 2, 3, 4], L). % Should return L = 4
% ?- reverse_list([a, b, c], Rev). % Should return Rev = [c, b, a]
% ?- is_palindrome([1, 2, 1]). % Should return true
% ?- is_palindrome([a, b, c]). % Should return false
% Queries
% ?- add(3, 4, Sum). % Should return Sum = 7
% ?- subtract(8, 5, Diff). % Should return Diff = 3
% ?- multiply(2, 6, Prod). % Should return Prod = 12
% ?- divide(10, 2, Quot). % Should return Quot = 5
Homework
-Try creating a simple Prolog program that models a family tree with at least three
generations.
-Write a prolog program that calculates factorial.