0% found this document useful (0 votes)
3 views

Prolog Sheet

The document contains three Prolog exercises: the first defines facts and rules to represent family relations and writes queries to find siblings and parents; the second writes predicates for common list manipulations; the third defines predicates for basic arithmetic operations like addition, subtraction, multiplication, and division.

Uploaded by

basma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Prolog Sheet

The document contains three Prolog exercises: the first defines facts and rules to represent family relations and writes queries to find siblings and parents; the second writes predicates for common list manipulations; the third defines predicates for basic arithmetic operations like addition, subtraction, multiplication, and division.

Uploaded by

basma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Exercise 1: Family Relations

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

Exercise 2: List Manipulation


Write Prolog predicates for common list manipulations such as finding the length, reversing a
list, and checking if a list is a palindrome.
% Finding the length of a list
list_length([], 0).
list_length([_|T], N) :- list_length(T, M), N is M + 1.
% Reversing a list
reverse_list([], []).
reverse_list([H|T], Rev) :- reverse_list(T, RT), append(RT, [H], Rev).
% Checking if a list is a palindrome
is_palindrome(List) :- reverse_list(List, List).

% 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

Exercise 3: Arithmetic Operations


Define Prolog predicates for basic arithmetic operations like addition, subtraction, multiplication,
and division.
% Addition
add(X, Y, Sum) :- Sum is X + Y.
% Subtraction
subtract(X, Y, Diff) :- Diff is X - Y.
% Multiplication
multiply(X, Y, Prod) :- Prod is X * Y.
% Division
divide(X, Y, Quotient) :- Y =\= 0, Quotient is X / Y.

% 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.

You might also like