Ai Lab2
Ai Lab2
Ai Lab2
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:
Code:
Output:
Output:
?- largest(12,56,90,R).
R = 90 .
?- largest(12,6,9,R).
R = 12 .
Subham Chowdhury
Output:
?- fib(5,R).
R=8.
?- fib(10,R).
R = 89
Output:
?- sum(5,R).
R = 15 .
?- sum(10,R).
R = 55
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
Output:
?- member(4,[1,2,3,4,5,6]).
true .
?- member(7,[1,2,3,4,5,6]).
false.
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.
Output:
?- insertelement(5,2,[4,3,7,8],Newlist).
Newlist = [4|(5| [3, 7, 8])]
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.
iv) On which days does professor roy teach the course ds?
?- course(ds,teacher(_,roy),time(X,_,_),_).
X = monday ;
X = wednesday.
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).
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.
Output:
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
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
Output:
Output: