Sei sulla pagina 1di 19

2180703:​ ​artificial Intelligence

Practical – 1
A. Write a prolog program to put facts indicating that sachin likes cricket, saurav likes cricket, raj
likes football, karan likes basketball, indra likes chess and add rule that parth likes whatever
saurav likes. Write goal for following queries:
a. Find the name of all players who like cricket.
b. Check whether raj likes cricket or not.
c. Display the list of all players with the game they like.
d. Display the name of players who likes any game except cricket.

a.

b.

160170116040: Patel Sakshi Jigar Kumar Page 1


2180703:​ ​artificial Intelligence

c.

d.

160170116040: Patel Sakshi Jigar Kumar Page 2


2180703:​ ​artificial Intelligence

B. I​ mplement prolog program that asks username and password from user and
display whether login is successful or not according to knowledge base considered
in the program.

a. Perform above program without recursion.

PROGRAM:

login(ram,1213).
login(shyam,45678).
login(sakshi,1111).
login(ronak,0000).

start :-
write('Enter the username :'),
read(U),

write('Enter the password :'),


read(P),

login(U,P),
write('Login successful').

start:-
write('Login not successful').

OUTPUT:

b. Perform above program with recursion.

160170116040: Patel Sakshi Jigar Kumar Page 3


2180703:​ ​artificial Intelligence

PROGRAM:

login(ram,1213).
login(shyam,45678).
login(sakshi,1111).
login(ronak,0000).

start :-
write('Enter the username :'),
read(U),

write('Enter the password :'),


read(P),

login(U,P),
write('Login successful'), nl,
write('Thank You').

start :-
write('Login not successful'), nl,
start.

OUTPUT:

Understanding of Implementation of Regularity Total Sign


Practical (2) Practical (6) (2) (10)

160170116040: Patel Sakshi Jigar Kumar Page 4


2180703:​ ​artificial Intelligence

160170116040: Patel Sakshi Jigar Kumar Page 5


2180703:​ ​artificial Intelligence

Practical – 2
Implement the following prolog programs

a. To find the greatest variable among the 3 variables.

PROGRAM:

max(P,Q,R):-P>Q,P>R,write('Larger number is '),write(P).

max(P,Q,R):-P<Q,Q>R,write('Larger number is '),write(Q).

max(P,Q,R):-R>Q,P<R,write('Larger number is '),write(R).

max(P,Q,R):-P=Q,P<R,write('Larger number is '),write(R).

max(P,Q,R):-P<Q,P=R,write('Larger number is '),write(Q).

max(P,Q,R):-Q=R,P>Q,write('Larger number is '),write(P).

max(P,Q,R):-P=Q,P>R,write('Larger numbers are '),write(P),write(' and '),write(Q).

max(P,Q,R):-P=R,Q<R,write('Larger numbers are '),write(P),write(' and '),write(R).

max(P,Q,R):-Q=R,P<R,write('Larger numbers are '),write(R),write(' and '),write(Q).

max(P,Q,R):-P=Q,P=R,write('All numbers are equal ').

OUTPUT:

160170116040: Patel Sakshi Jigar Kumar Page 6


2180703​: a​ rtificial Intelligence

b. ​To find a factorial of a given number

PROGRAM:

fact(0,1).
fact(N,F):-
(
N>0 ->
(
N1 is N-1,
fact(N1,F1),
F is N*F1
)
;
N<0 ->
(
N1 is N+1,
fact(N1,F1),
F is N*F1
)
).

OUTPUT:

160170116040: Patel Sakshi Jigar Kumar Page 7


2180703​: a​ rtificial Intelligence

c. To check whether the given number is palindrome or not.

PROGRAM:

start:-
write("Enter any number : "),
read(N),
No is N,
Y is 0,
palindrome(No,N,Y).

palindrome(No,N,Y):-
N > 0,
Yy is (Y*10 + N mod 10),
Nn is (round(N/10)),
palindrome(No,Nn,Yy).

palindrome(No,N,Y):-
No=Y,
write("Palindrome Number.").

palindrome(_,_,_):-
write("Not a palindrome number.").

OUTPUT:

160170116040: Patel Sakshi Jigar Kumar Page 8


2180703​: a​ rtificial Intelligence

d. To check whether the given number is prime or not.

PROGRAM:

divisible(X,Y) :- 0 is X mod Y, !.

divisible(X,Y) :- X > Y+1, divisible(X, Y+1).

isPrime(2) :- true,!.
isPrime(X) :- X < 2,!,false.
isPrime(X) :- not(divisible(X, 2)).

OUTPUT:

Understanding of Implementation of Regularity Total Sign


Practical (2) Practical (6) (2) (10)

160170116040: Patel Sakshi Jigar Kumar Page 9


2180703​: a​ rtificial Intelligence

Practical – 3
Write a separate prolog programs to demonstrate the fail and cut(!) predicates.

CUT Predicate:​ When interpreter comes across a ‘cut’ the affect is that all the alternative solutions
of the goal waiting to be tried are abounded thereby reducing the size of the search tree.

PROGRAM:

a(X) :- b(X),!,c(X).
a(X) :- d(X).

b(1).
b(4).
c(1).
c(3).
d(4).

OUTPUT:

FAIL Predicate:​ It is used to force a rule to fail under certain conditions. Predicate Fail tells the
Prolog interpreter to fail a particular goal and subsequently forces backtracking.

PROGRAM:

a(X) :- b(X),!,c(X),fail.
a(X) :- d(X).

b(1).
b(4).
c(1).
c(3).
d(4).

160170116040: Patel Sakshi Jigar Kumar Page 10


2180703​: a​ rtificial Intelligence

OUTPUT:

Understanding of Implementation of Regularity Total Sign


Practical (2) Practical (6) (2) (10)

160170116040: Patel Sakshi Jigar Kumar Page 11


2180703​: a​ rtificial Intelligence

Practical – 4
Write a prolog program for Medical Diagnosis system.

PROGRAM:

go :-
write('What is the patient''s name? '),
readln(Patient),%get_single_char(Code),
hypothesis(Patient,Disease),
write_list([Patient,', probably has ',Disease,'.']),nl.

go :-
write('Sorry, I don''t seem to be able to'),nl,
write('diagnose the disease.'),nl.

symptom(Patient,fever) :-
verify(Patient," have a fever (y/n) ?").
symptom(Patient,rash) :-
verify(Patient," have a rash (y/n) ?").
symptom(Patient,headache) :-
verify(Patient," have a headache (y/n) ?").
symptom(Patient,runny_nose) :-
verify(Patient," have a runny_nose (y/n) ?").
symptom(Patient,conjunctivitis) :-
verify(Patient," have a conjunctivitis (y/n) ?").
symptom(Patient,cough) :-
verify(Patient," have a cough (y/n) ?").
symptom(Patient,body_ache) :-
verify(Patient," have a body_ache (y/n) ?").
symptom(Patient,chills) :-
verify(Patient," have a chills (y/n) ?").
symptom(Patient,sore_throat) :-
verify(Patient," have a sore_throat (y/n) ?").
symptom(Patient,sneezing) :-
verify(Patient," have a sneezing (y/n) ?").
symptom(Patient,swollen_glands) :-
verify(Patient," have a swollen_glands (y/n) ?").

ask(Patient,Question) :-
write(Patient),write(', do you'),write(Question),
read(N),
( (N == yes ; N == y)
->
assert(yes(Question)) ;
assert(no(Question)), fail).
:- dynamic yes/1,no/1.

verify(P,S) :-

160170116040: Patel Sakshi Jigar Kumar Page 12


2180703​: a​ rtificial Intelligence

(yes(S) -> true ;


(no(S) -> fail ;
ask(P,S))).

undo :- retract(yes(_)),fail.
undo :- retract(no(_)),fail.
undo.

hypothesis(Patient,german_measles) :-
symptom(Patient,fever),
symptom(Patient,headache),
symptom(Patient,runny_nose),
symptom(Patient,rash).

hypothesis(Patient,common_cold) :-
symptom(Patient,headache),
symptom(Patient,sneezing),
symptom(Patient,sore_throat),
symptom(Patient,runny_nose),
symptom(Patient,chills).

hypothesis(Patient,measles) :-
symptom(Patient,cough),
symptom(Patient,sneezing),
symptom(Patient,runny_nose).

hypothesis(Patient,flu) :-
symptom(Patient,fever),
symptom(Patient,headache),
symptom(Patient,body_ache),
symptom(Patient,conjunctivitis),
symptom(Patient,chills),
symptom(Patient,sore_throat),
symptom(Patient,runny_nose),
symptom(Patient,cough).

hypothesis(Patient,mumps) :-
symptom(Patient,fever),
symptom(Patient,swollen_glands).

hypothesis(Patient,chicken_pox) :-
symptom(Patient,fever),
symptom(Patient,chills),
symptom(Patient,body_ache),
symptom(Patient,rash).

write_list([]).
write_list([Term| Terms]) :-
write(Term),

160170116040: Patel Sakshi Jigar Kumar Page 13


2180703​: a​ rtificial Intelligence

write_list(Terms).

response(Reply) :-
readchar(Reply),
write(Reply),nl.

OUTPUT:

Understanding of Implementation of Regularity Total Sign


Practical (2) Practical (6) (2) (10)

160170116040: Patel Sakshi Jigar Kumar Page 14


2180703​: a​ rtificial Intelligence

160170116040: Patel Sakshi Jigar Kumar Page 15


2180703​: a​ rtificial Intelligence

Practical – 5
Implement following prolog programs based on list.

a. To display first element of a list.

PROGRAM:

first([H|_]):-
write("First element of the list is ":H),nl.

OUTPUT:

b. To display last element of a list.

PROGRAM:

last([X]):-
write("Last element is : ":X),nl.
last([_|Tail]):-
last(Tail).

OUTPUT:

160170116040: Patel Sakshi Jigar Kumar Page 16


2180703​: a​ rtificial Intelligence

c. To display all element of a list

PROGRAM:

printlist([X|List]) :-
write(X),nl,
printlist(List).

OUTPUT:

d. To display elements up to specified index of a list.

PROGRAM:

display(L):-
write("Enter starting index : "),
read(Si),
write("Enter Ending index : "),
read(Ei),Si>0,count(L,Si,Ei,0),!.

display(_):- write("Invalid Index").

count([H|L],S,E,C):-
H\=[],
Cc is C+1,
((Cc>=S,Cc=<E)->write(H),nl,count(L,S,E,Cc);(Cc>E)->break;count(L,S,E,Cc)).

OUTPUT:

160170116040: Patel Sakshi Jigar Kumar Page 17


2180703​: a​ rtificial Intelligence

e. To count number of elements in a list.

PROGRAM:

count([],0).
count([_|T],C) :- count(T,X), C is X + 1.

OUTPUT:

f. To count odd and even elements of a list.

PROGRAM:

count_both(L):-
count(L,0,0).
count([H|T],C1,C2):-
H\=[],(A is H mod 2, A=0, Cc is C1+1, count(T,Cc,C2);Cd is C2+1, count(T,C1,Cd)),!.

count(_,C1,C2):-
write("No. of even is: "),write(C1),nl,write("No. of odd is: "),write(C2).

OUTPUT:

Understanding of Implementation of Regularity Total Sign


Practical (2) Practical (6) (2) (10)

160170116040: Patel Sakshi Jigar Kumar Page 18


2180703​: a​ rtificial Intelligence

160170116040: Patel Sakshi Jigar Kumar Page 19

Potrebbero piacerti anche