Você está na página 1de 11

1.

Define the relationship in prolog :


Grandparent, Grandchild, Father, Mother, Brother, Sister, Common Parent
where you are given a family tree.

Solution: A Family tree is given below:


Pa To
m m

Bob Liz

An Pa
n t

Ji
m

In prolog, this family tree is represented by the following clauses. Each of these
clauses declares one fact about the parent relation and some clauses define whether
the tree elements is male or female.

parent(pam,bob).
parent(tom,bob).
parent(tom,liz).
parent(bon,ann).
parent(bob,pat).
parent(pat,jim).
female(pam).
female(ann).
male(tom).
male(liz).
male(bob).
male(pat).
male(jim).

e.g parent(tom,bob). is a particular instance of the parent relation. Such as


instance is called relationship.
In prolog, Grandparent, Grandchild, Father, Mother, Brother, Sister, Common
Parent relationships are shown below in a single file.

# vi family.pl

parent(pam,bob).
parent(tom,bob).
parent(tom,liz).
parent(bon,ann).
parent(bob,pat).
parent(pat,jim).
female(pam).
female(ann).
male(tom).
male(liz).
male(bob).
male(pat).
male(jim).
grandparent(X,Y):-parent(Z,Y),parent(X,Z).
grandchild(X,Y):-parent(Y,Z),parent(Z,X).
father(X,Y):-parent(X,Y),male(X).
mother(X,Y):-parent(X,Y),female(X).
brother(X,Y):-parent(Z,Y),parent(Z,X),male(X).
sister(X,Y):-parent(Z,Y),parent(Z,X),female(X).
commonparent(Z,X,Y):-parent(Z,X),parent(Z,Y).

After saving and exit this file, we type gprolog in the prompt:-
# gprolog

Compile it using following convension:-


?-[family.pl].
Now we perform the following queries:-
i) Suppose we want to know the Grandparent of jim , then
?-grandparent(X,jim).
X=bob

ii) Suppose we want to know the Grandchild of tom,then


?-grandchild(X,tom).
X=ann;
X=pat

iii) Suppose we want to know the father’s name of bob, then


?-father(X,bob).
X=tom

iv) Suppose we want to know the mother’s name of bob, then


?-mother(X.bob).
X=Pam

v) Suppose we want to know brother’s name of liz, then


?-brother(X,liz).
X=bob

vi) Suppose we want to know sister’s name of pat, then


?-sister(X,pat).
X=ann

vii) Suppose we want to know the common parent of ann & pat, then
?-common_parent(Z,ann,pat).
Z=bob
2. Find factorial, fibbonacci ,GCD using recursion.

Solution:

Prolog program of factorial:

# vi factorial.pl

fact(0,1).
fact(N,R):-N1 is N-1,fact(N1,R1),R is N*R1.

After saving and exit this file, we type gprolog in the prompt:-
# gprolog

Compile it using following convension:-


?-[factorial.pl].

Output:
?-fact(5,R).
R=120

Prolog program of Fibonacci:

# vi fibonacci.pl

fibo(1,0).
fibo(2,1).
fibo(N,R):-M is N-1,X is N-2,fibo(M,Q),fibo(X,Z),R is Q+Z.

After saving and exit this file, we type gprolog in the prompt:-
# gprolog

Compile it using following convension:-


?-[fibonacci.pl].

Output:
?-fibo(5,R).
R=3
Prolog program of GCD:

# vi gcd.pl

gcd(X,Y,G):-Y>X,gcd(Y,X,G).
gcd(X,0,X).
gcd(X,Y,G):-Y>0,R is X mod Y,gcd(Y,R,G).

After saving and exit this file, we type gprolog in the prompt:-
# gprolog

Compile it using following convension:-


?-[gcd.pl].

Output:
?-gcd(12,30,G).
G=6

3. Write a program to generate numbers in a given range.

Solution:
# vi gen_range.pl

gen_range(M,N,[M|L]):-M<N, M1 is M+1, gen_range(M1,N,L).


gen_range(N,N,[N]).

After saving and exit this file, we type gprolog in the prompt:-
# gprolog

Compile it using following convension:-


?-[gen_range.pl].

Output:
?-gen_range(10,15,Y).
Y=[10,11,12,13,14,15]
4. Write a prolog program to compute the sum of the square of the integer
series [12+22+32+42……].

Solution:

# vi sum.pl

sum(1,1).
sum(N,R):-N1 is N-1,N2 is N*N,sum(N1,R1),R is N2+R1.

After saving and exit this file, we type gprolog in the prompt:-
# gprolog

Compile it using following convension:-


?-[sum.pl].

Output:
?-sum[3,R].
R=14
5. Write a prolog program :
i) To check whether an element is a member of the list.
ii) Multiply the elements of integer list.
iii) Length of the list.
iv) Concatenate two lists.
v) Find the maximum element of a given list.
vi) Determine the last and first element of a given list.
vii) Insert an element into a list
viii)Delete first item from a list.
ix) Delete last item from a list.
x) Delete all occurance of an element from a list.

Solution:

# vi list.pl

member(X,[X|T]).
member(X,[H|T]):-member(X,T).

mul_list([ ],1).
mul_list([H|L],X):-mul_list(L,X2), X is X2*H.

length_list([ ],0).
length_list([H|T],L):-length_list(T,L1),L is L1+1.

conc([ ],L,L).
conc([X|L1],L2,[X|L3]):-conc(L1,L2,L3).

list_max([H|L],X):-process_max(L,H,X).
process_max([ ],M,M).
process_max([H|L],M,X):-(H>M, M2 is H, process_max (L,M2,X));
(process_max(L,M,X)).

fl_list([H|T],F,L):-F is H, process_last(T,L).
process_last([X],X).
process_last([H|L],X):-process_last(L,X).

add(X,L,[X|L]).

del_first([H|L],L).
del_last([X],[ ]).
del_last([H|L1],[H|L2]):-del_last(L1,L2).

del_occurence(X,[],[]).
del_occurence(X,[X|L1],L2):-del_occurence(X,L1,L2).
del_occurence(X,[H|L1],[H|L2]):-del_occurence(X,L1,L2).

After saving and exit this file, we type gprolog in the prompt:-
# gprolog

Compile it using following convension:-


?-[list.pl].

Output:

i) ?-member(3,[3,4,5]).
Yes
ii) ?-mul_list([2,5,6],X).
X=60
iii) ?-length_list([1,2,3,4,5],L).
L=5
iv) ?-conc([1,2,3],[4,5],L).
L=[1,2,3,4,5]
v) ?-list_max([5,4,3,6,8],X).
X=8
vi) ?-fl_list([1,2,3,4,5],X,Y).
X=1
Y=5
vii) ?-add(2,[3,4,5,6],Y).
Y=[2,3,4,5,6]
viii) ?-del([2,3,4,5,6],Y).
Y=[3,4,5,6]
ix) ?-del_last([1,2,3,4,5],X).
X=[1,2,3,4]
x) ?-del_occurence(2,[2,3,2,4,2,5],X).
X=[3,4,5]
6. Write a program in prolog to perform preorder, inorder, postorder traversal of a
binary tree.

Solution: The binary tree is given shown below:


3
0

2 6
5 0

1 7
0 0

Pre-order traversal:

# vi preorder.pl

conc([ ],L,L).
conc([X|L1],L2,[X|L3]):-conc(L1,L2,L3).
b_tree(30,b_tree(25,b_tree(10,void,void),void),b_tree(60,void,b_tree(70,
void,void))).
pre_btree(void,[ ]).
pre_btree(b_tree(X,L,R),Y):-pre_btree(L,L1),pre_btree(R,R1),conc([X|
L1],R1,Y).

After saving and exit this file, we type gprolog in the prompt:-
# gprolog

Compile it using following convension:-


?-[preorder.pl].

Output:
?-pre_btree( b_tree( 30,b_tree( 25,b_tree( 10,void,void),void),b_tree(60, void,
b_tree(70, void,void))),Y).

Y=[30,25,10,60,70]
In-order traversal:

# vi inorder.pl

conc([ ],L,L).
conc([X|L1],L2,[X|L3]):-conc(L1,L2,L3).
b_tree(30,b_tree(25,b_tree(10,void,void),void),b_tree(60,void,b_tree(70,
void,void))).
in_btree(void,[ ]).
in_btree(b_tree(X,L,R),Y):-in_btree(L,L1),in_btree(R,R1),conc(L1,[X|
R1],Y).

After saving and exit this file, we type gprolog in the prompt:-
# gprolog

Compile it using following convension:-


?-[inorder.pl].

Output:
?-in_btree( b_tree( 30,b_tree( 25,b_tree( 10,void,void),void),b_tree(60, void,
b_tree(70, void,void))),Y).

Y=[10,25,30,60,70]
Post-order traversal:

# vi postorder.pl

conc([ ],L,L).
conc([X|L1],L2,[X|L3]):-conc(L1,L2,L3).
b_tree(30,b_tree(25,b_tree(10,void,void),void),b_tree(60,void,b_tree(70,
void,void))).
post_btree(void,[ ]).
post_btree(b_tree(X,L,R),Y):-post_btree(L,L1),post_btree(R,R1),conc(R1,
[X],R2),conc(L1,R2,Y).

After saving and exit this file, we type gprolog in the prompt:-
# gprolog

Compile it using following convension:-


?-[postorder.pl].

Output:
?-post_btree( b_tree( 30,b_tree( 25,b_tree( 10,void,void),void),b_tree(60, void,
b_tree(70, void,void))),Y).

Y=[10,25,70,60,30]

Você também pode gostar