Predicates Raining Wet Notplay Clauses Raining. Wet:-Raining. Notplay: - Wet
Predicates Raining Wet Notplay Clauses Raining. Wet:-Raining. Notplay: - Wet
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
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
OUTPUT:
Goal: member(b,[a,b,c])
b is the member of list..? : Yes
ii.
OUTPUT:
Goal: append([a,b,c],[d,e,f,g],X)
X=["a","b","c","d","e","f","g"]
1 Solution
iii.
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.
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