Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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.
c.
d.
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.
PROGRAM:
login(ram,1213).
login(shyam,45678).
login(sakshi,1111).
login(ronak,0000).
start :-
write('Enter the username :'),
read(U),
login(U,P),
write('Login successful').
start:-
write('Login not successful').
OUTPUT:
PROGRAM:
login(ram,1213).
login(shyam,45678).
login(sakshi,1111).
login(ronak,0000).
start :-
write('Enter the username :'),
read(U),
login(U,P),
write('Login successful'), nl,
write('Thank You').
start :-
write('Login not successful'), nl,
start.
OUTPUT:
Practical – 2
Implement the following prolog programs
PROGRAM:
OUTPUT:
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:
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:
PROGRAM:
divisible(X,Y) :- 0 is X mod Y, !.
isPrime(2) :- true,!.
isPrime(X) :- X < 2,!,false.
isPrime(X) :- not(divisible(X, 2)).
OUTPUT:
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).
OUTPUT:
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) :-
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),
write_list(Terms).
response(Reply) :-
readchar(Reply),
write(Reply),nl.
OUTPUT:
Practical – 5
Implement following prolog programs based on list.
PROGRAM:
first([H|_]):-
write("First element of the list is ":H),nl.
OUTPUT:
PROGRAM:
last([X]):-
write("Last element is : ":X),nl.
last([_|Tail]):-
last(Tail).
OUTPUT:
PROGRAM:
printlist([X|List]) :-
write(X),nl,
printlist(List).
OUTPUT:
PROGRAM:
display(L):-
write("Enter starting index : "),
read(Si),
write("Enter Ending index : "),
read(Ei),Si>0,count(L,Si,Ei,0),!.
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:
PROGRAM:
count([],0).
count([_|T],C) :- count(T,X), C is X + 1.
OUTPUT:
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: