PROLOG Programs
1. To express the following information and check whether it is possible to play now :
If it is raining then the ground will be wet.
If the ground is wet then it is not possible to play.
It is raining now
predicates
raining
wet
notplay
clauses
raining.
wet :- raining.
notplay :- wet.
OUTPUT :
Goal: notplay
Yes
2. To express the following information and check whether John can marry Mary :
Mary Likes flowers. John Likes wine. John likes anyone who likes wine.
Mary likes anyone who likes flowers. Anyone who likes wine likes flowers.
Those who like each other can marry
predicates
likes(symbol,symbol)
marry(symbol,symbol)
clauses
likes(john,wine).
likes(mary,flowers).
likes(john,X) :- likes(X,wine).
likes(mary,Y) :- likes(Y,flowers).
likes(X,wine) :- likes(X,flowers).
marry(X,Y) :- likes(X,Y).
marry(X,Y) :- likes(Y,X).
OUTPUT:
Goal: marry(john,mary)
Yes
3. To store the area and population of various countries and define a predicate to calculate the
population density using the following formula
Population density = Population / Area
predicates
pop(symbol,integer)
area(symbol,integer)
density(symbol,integer)
clauses
pop(usa,203).
pop(india,548).
pop(china,800).
pop(brazil,108).
area(usa,3).
area(india,1).
area(china,4).
area(brazil,3).
density(X,Y) :- pop(X,P),
area(X,A),
Y=P/A.
OUTPUT:
Goal: density(china,X)
X=200
1 Solution
4. To calculate the sum of N natural numbers.
domains
N,N1,R1=integer
predicates
sum(integer,integer)
clauses
sum(N,1) :- N<=1,
!.
sum(N,R) :- N1=N-1,
sum(N1,R1),
R=R1+N.
OUTPUT:
Goal: sum(5,X)
X=15
1 Solution
5. To simulate a medical diagnostic model, which diagnosis the child deseases.
domains
name,disease=symbol
predicates
symptoms(name,disease)
run
clauses
run:write("Enter name of disease : "),
readln(Dis),
symptoms(Name,Dis),
write(Name, " is suffering from ",Dis),nl.
symptoms(john,fever).
symptoms(sachin,cold).
symptoms(jack,headache).
symptoms(aasin,cough).
OUTPUT:
Goal: run
Enter name of disease :
cold
sachin is suffering from
cold
Yes
6. To login routine without using recursion
domains
name,password=symbol
predicates
nondeterm getinput(name,password)
nondeterm logon
nondeterm user(name,password)
clauses
logon:getinput(_,_),
write("You are now logged in"),nl.
logon:write("Sorry you cannot login "),nl.
getinput(Name,Password):write("Enter the name : "),
readln(Name),
write("Enter the password "),
readln(Password),
user(Name,Password).
user(adan,ad).
user(jack,ja).
user(smith,sm).
OUTPUT:
Goal: logon
Enter the name :smith
Enter the passwords
Sorry you cannot login
Yes
Goal: logon
Enter the name : smith
Enter the password: sm
You are now logged in
Yes
7. To login routine with using recursion
domains
name,password=symbol
predicates
nondeterm getinput(name,password)
nondeterm logon
nondeterm user(name,password)
nondeterm repeat
clauses
repeat.repeat:repeat.logon:getinput(_,_),
write("You are now logged in"),nl.
logon:repeat,
write("Sorry you cannot login"),nl,
getinput(_,_),
write("You are logged in"),nl.
getinput(name,password) :write("Enter your name : "),
readln(Name),
write("Enter your Password : "),
readln(Password),
user(Name,Password).
user(fedrich,fr).
user(michel,ml).
user(enrich,eh).
user(smith,sm).
OUTPUT:
Goal: logon
Enter your name : smith
Enter your Password : ss
Sorry you cannot login
Enter your name : smith
Enter your Password : sm
You are logged in
Yes
8. To Perform the following operations on a list
i.
To check whether a given item is an element of the list.
domains
namelist=symbol*
name=symbol
predicates
nondeterm member(symbol,namelist)
clauses
member(X,[X|_]).
member(X,[_|Tail]) :write(X," is the member of list ..? : "),
member(X,Tail).
OUTPUT:
Goal: member(b,[a,b,c])
b is the member of list..? : Yes
ii.
For appending two lists.
domains
namelist=symbol*
predicates
nondeterm append(namelist,namelist,namelist)
clauses
append([],List,List).
append([X|List1],List2,[X|List3]):append(List1,List2,List3).
OUTPUT:
Goal: append([a,b,c],[d,e,f,g],X)
X=["a","b","c","d","e","f","g"]
1 Solution
iii.
Removing an element from a list.
domains
List=integer*
N=integer
predicates
nondeterm del(List,List,N)
clauses
del([Head|Tail],PT,N):Head=N,
!,
del(Tail,PT,N).
del([Head|Tail],[Head|PT],N):del(Tail,PT,N).
del([],[],_).
OUTPUT:
Goal: del([1,2,3],X,2)
X=[1,3]
1 Solution
iv.
Finding last element of a list.
domains
namelist=symbol*
element=symbol
predicates
nondeterm last_element(namelist,element)
clauses
last_element([Head],X):X=Head.
last_element([_|Tail],X):last_element(Tail,X).
OUTPUT:
Goal: last_element([a,b,c,d],X)
X=d
1 Solution
v.
Reversing a list.
domains
L=symbol*
predicates
rev(L,L)
reverse(L,L,L)
clauses
rev(IN,OUT):- reverse(IN,[],OUT).
reverse([],IN,IN).
reverse([H|T],L1,L2):-reverse(T,[H|L1],L2).
OUTPUT:
Goal: rev([a,b,c],X)
X=["c","b","a"]
1 Solution
9. A directed graph is to be stored in a PROLOG program using the predicate edge(X,Y) to
indicate whether there is an edge from vertex X to vertex Y. Write a PROLOG program
with necessary predicate to define path between two nodes.
domains
X,N1,N2=symbol
predicates
edge(symbol,symbol)
path(symbol,symbol)
clauses
edge(a,b).
edge(a,e).
edge(b,d).
edge(b,c).
edge(c,d).
edge(e,b).
path(N1,N2) :- edge(N1,N2).
path(N1,N2) :- edge(N1,X),
edge(X,N2).
OUTPUT:
Goal: path(b,Y)
Y=d
Y=c
Y=d
3 Solutions