Ai Lab2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Subham Chowdhury

1. The following facts are given for a particular family tree:


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

man(bob). woman(pam).
man(tom). woman(ann).
man(jim). woman(liz).

Now formulate the relations GRANDPARENT (X, Y), ANCESTOR (X, Y) and SISTER (X, Y) to find who is the
grandparent of whom? who is the ancestor of whom? who is the sister of whom respectively?

Code:

Output:

2. A person may steal Y if X is a thief, X is a man, X likes Y & Y is valuable. Given the following facts, define a predicate
steal (X, Y) & determines who steals what?
man(john). woman(mary).
valuable(gold).
likes(john, gold). likes(john, mary).
thief(john). thief(mary).

Code:
man(john).
woman(mary).
valuable(gold).
likes(john, gold).
likes(john, mary).
thief(john).
thief(mary).
steal(X,Y):-thief(X),man(X),likes(X,Y),valuable(Y).
thief(X),man(X),likes(X,Y),valuable(Y).
Subham Chowdhury

Output:

3. Consider the following facts & rules:


Facts are:
a) Hardware is easy course b) Books for hardware are available
c) Logic is not easy course d) Graphics is easy course
e) Graphics has 8 credits f) Graphics has a lab component
g) Books for Database are available h) Mary takes compiler
Rules are:
Rule1: X takes Y, if Y is easy course and books for Y are available
Rule2: X takes Y, if Y has 8 credits and Y has lab component
Write Prolog Program to answer the following queries:

1. Does Mary take graphics course?


2. Which courses Mary take?
3. Who takes Graphics course?

Code:

Output:

4. Write a prolog program to find the maximum of 3 numbers.


Code:
largest(A,B,C,R):-A>B,A>C,R is A.
largest(A,B,C,R):-B>A,B>C,R is B.
largest(A,B,C,R):-C>A,C>B,R is C.
largest(A,B,C,R):-A=B,A=C,R is A.

Output:
?- largest(12,56,90,R).
R = 90 .
?- largest(12,6,9,R).
R = 12 .
Subham Chowdhury

5. Write a prolog program to find the nth Fibonacci term.


Code:
fib(0,1).
fib(1,1).
fib(N,F):- N>1, N1 is N-1, N2 is N-2, fib(N1,F1), fib(N2,F2), F is F1+F2.

Output:
?- fib(5,R).
R=8.
?- fib(10,R).
R = 89

6. Write a prolog program to find the GCD of 2 numbers.


Code:
gcd(X,Y):-X=Y, write('GCD = '),write(X);
X=0, write('GCD = '),write(Y);
Y=0, write('GCD = '),write(X);
Y>X, Y1 is Y-X, gcd(X,Y1);
X>Y, Y1 is X-Y, gcd(Y1,Y).
Output:
?- gcd(9,12).
GCD = 3
true .
?- gcd(7,12).
GCD = 1
true

7. Write a prolog program to find the sum of first N natural numbers.


Code:
sum(0,0).
sum(N,R):- N>0, N1 is N-1, sum(N1,R1), R is R1+N.

Output:
?- sum(5,R).
R = 15 .
?- sum(10,R).
R = 55

8. Consider an undirected graph represented by the following facts:


arc(a, b).
arc(b, c).
arc(a, c).
arc(a, d).
arc(b, e).
arc(e, f).
arc(b, f).
arc(f, g).
Now answer the following questions:
i. Find all the arcs in the given graph
ii. Find those nodes having edge to node ‘a’
iii. Write a Prolog Program to check whether there is any path between two nodes
in a graph.
Subham Chowdhury

Code:
arc(a,b).
arc(b,c).
arc(a,c).
arc(a,d).
arc(b,e).
arc(e,f).
arc(b,f).
arc(f,g).
path(X,X).
path(X,Y):-arc(X,Z),path(Z,Y).

Output:
Subham Chowdhury

9. Write a prolog program to search an element in a list.


Code
member(X,[X/L]).
member(X,[Y/L]):- member(X,L).

Output:
?- member(4,[1,2,3,4,5,6]).
true .
?- member(7,[1,2,3,4,5,6]).
false.

10. Write a prolog program to find the length of the list.


Code:
len(0,[]).
len(L,[X|Y]):- len(L1,Y), L is L1+1.

Output:
?- len(L,[1,a,u,6,7,8]).
L=6.
?- len(L,[]).
L=0

11. Write a prolog program to find the sum of the elements present in the list.
Code:
sum([],0).
sum([X|Y],R):- sum(Y,R1), R is R1+X.

Output:
?- sum([1,2,6,7],R).
R = 16.

12. Write a prolog program to insert an element in a list.


Code:
insertelement(X,1,L,(X|L)).
insertelement(X,P,[Y|L],[Y|L1]):-P>1,P1 is P-1, insertelement(X,P1,L,L1).

Output:
?- insertelement(5,2,[4,3,7,8],Newlist).
Newlist = [4|(5| [3, 7, 8])]

13. Write a prolog program to delete an element by element in a list.


Code:
del(X,[X|L],L).
del(X,[Y|L],[Y|L1]):- del(X,L,L1).

Output:
?- del(3,[1,6,7,8,3,4],R).
R = [1, 6, 7, 8, 4] .
Subham Chowdhury

14. Write a prolog program to printing the last and second last element in a list.
Code:
print([X],X).
print([X,Y],X).
print([X|Y],R):-print(Y,R).

Output:
?- print([1,2,3,5,70,8],X).
X = 70 ;
X=8.

15. Write a prolog program to print the element in the even position of the list.
Code:
even([X,Y|Z],Y).
even([X,Y|Z],R):-even(Z,R).

Output:
?- even([1,3,5,4,6,7,8],R).
R=3;
R=4;
R=7;
false.

16. Structured Database:


Knowledge Base:
course(ds,teacher(abc,roy),time(monday,9,10),location(ict,123)).
course(ds,teacher(abc,roy),time(wednesday,9,10),location(ict,134)).
course(logic,teacher(abc,roy),time(friday,9,10),location(ict,321)).
course(oops,teacher(xyz,choudhury),time(monday,10,12),location(ict,501)).
course(logic,teacher(opr,das),time(tuesday,3,5),location(ict,234)).

duration():-course(C,_,time(_,A,B),_), X is B-A, write(C),write(' '),write(X), write(' '),write('hrs').

i) Who will taken the ds?


?- course(ds,teacher(A,B),_,_).
A = abc,
B = roy ;
A = abc,
B = roy.

ii) Who teaches the course logic?


?- course(logic,teacher(A,B),_,_).
A = abc,
B = roy ;
A = opr,
B = das.

iii) Which courses does professor choudhury teach?


?- course(X,teacher(_,choudhury),_,_).
X = oops.
Subham Chowdhury

iv) On which days does professor roy teach the course ds?
?- course(ds,teacher(_,roy),time(X,_,_),_).
X = monday ;
X = wednesday.

v) What is the duration of each course present in your knowledge base?


?- duration().
ds 1 hrs
true ;
ds 1 hrs
true ;
logic 1 hrs
true ;
oops 2 hrs
true ;
logic 2 hrs
true.

17. A house has six rooms & eight doors as shown in the figure below. Only room 6 has atelephone. A person is
standing at position ‘entry’. He wants to reach to the telephone after going through a number of doors. Write a
Prolog program to find out all the sequences according to which he should traverse the rooms along with their cost.
Code:
door(entry,one,1).
door(one,two,1).
door(one,four,1).
door(two,three,1).
door(three,four,1).
door(three,six,1).
door(four,five,1).
door(five,six,1).

connected(X,Y,L) :- door(X,Y,L) ; door(Y,X,L).


path(A,B,Path,Len) :-travel(A,B,[A],Q,Len),
travel(A,B,[A],Q,Len), reverse(Q,Path).
travel(A,B,P,[B|P],L) :-connected(A,B,L).
travel(A,B,Visited,Path,L) :- connected(A,C,D),
C \== B, \+member(C,Visited), travel(C,B,[C|Visited],Path,L1), L is D+L1.

Output:
Subham Chowdhury

18. Write a prolog program to design a login module which asks user his login name followed by password. The user
will be kept on asking the login name until the user gives the valid login name. Once it is correct then the user will be
asked to provide the respective password. If password is not correct, it keeps on asking till correct password is given.
Do this program by using CUT and by not using CUT.

Code(By using CUT):


getnamePassword(UName,UPass):- write('Enter Username:'),read(N),getInfo(N,UName), write('Enter Password:
'),read(P),getInfo(P,UPass).
getInfo(N,UName):- N \= UName,write('Invalid login password, tryagain...'),nl, write('\nReEnter
nReEnter your password
:'),read(X),getInfo(X,UName),!.
getInfo(N,UName):- N == UName,write('Correct'),nl.

Output:

Code(By not using CUT):


namePassword(Uname,Upass):- write('Enter name:'),read(N),getInfo(N,Uname),write('Enter Password:
'),read(P),getInfo(P,Upass).
getInfo(N,Uname):- N \= Uname,write('Invalid login password, tryagain...'),nl,write('ReEnter your password :
'),read(X),getInfo(X,Uname).
getInfo(N,Uname):- N == Uname,write('Correct'),nl.

Output:

19. Write a prolog program to find the reverse of a given list, using accumulator and another program will be by not
using accumulator.
Code(Using accumulator):
accrev([],A,A).
accrev([H|T],A,Acc):-accrev(T,A,[H|Acc]).
accrev(T,A,[H|Acc]).

Output:
Subham Chowdhury

Code(Without Using accumulator):


reverse([],[]).
reverse([X],[X]).
reverse([X|X1],R):-reverse(X1,T).
append(T,[X],R).

Output:

20. The program has "in mind" a word and you have to guess this word: letter by letter. Unless you don't guess the
first letter the program does not ask for the next letter. If you guess the first letter the program says: OK and asks for
the next one.(Hint: use the built-in relation atom_chars()).
Code:

Output:
Subham Chowdhury

21. Consider an undirected graph represented by the following facts:


arc(a, b). arc(b, c).
arc(a, c). arc(a, d).
arc(b, e). arc(e, f).
arc(b, f). arc(f, g).
Write a program in Prolog to return a list containing the nodes of the graph
(Hint: use the built-in relation setof()).
Code:
arc(a,b).
arc(b,c).
arc(a,c).
arc(a,d).
arc(b,e).
arc(e,f).
arc(b,f).
arc(f,g).
node(N):-arc(N,_); arc(_,N).
nodelist(L):-setof(N,node(N),L).

Output:

22. Write a Prolog program to concatenate two given lists.


Code:
concatenate_lists([],L,L).
concatenate_lists(L,[],L).
concatenate_lists([X1|L1],L2,[X1|L3]):- concatenate_lists(L1,L2,L3).

Output:

You might also like