ADDTION OF TWO NUMBERS:
predicates
add
clauses
add:-write("input first number"),
readint(X),
write("input second number"),
readint(Y),
Z=X+Y,write("output=",Z).
OUTPUT : add
predicates
small(symbol)
large(symbol)
color(symbol,symbol)
clauses
small(rat).
small(cat).
large(lion).
color(dog,black).
color(rabbit,white).
color(X,dark):-
color(X,black);color(X,brown).
OUTPUT: small(X)
Program to read address of a person using compound variable .
domains
person=address(name,street,city,state,zip)
name,street,city,state,zip=String
predicates
readaddress(person)
go
clauses
go:-
readaddress(Address),nl,write(Address),nl,nl,write("Accept(y/n)?"),readchar(Reply),Reply='y',!.
go:-
nl,write("please re-enter"),nl,go.
readaddress(address(N,street,city,state,zip)):-
write("Name:"),readln(N),
write("Street:"),readln(street),
write("City:"),readln(city),
write("State:"),readln(state),
write("Zip:"),readln(zip).
OUTPUT : go
Program of fun to show concept of cut operator .
predicates
fun(integer,integer)
clauses
fun(Y,1):-Y<3,!.
fun(Y,2):-Y>3,Y<=10,!.
fun(Y,3):-Y>10,!.
Program to count number of elements in a list .
domains
x=integer
list=integer*
predicates
count(list,x)
clauses
count([],0).
count([_|T],N):-count(T,N1),N=N1+1.
Output : count([1,2,3,4,5,6],X)
Program to reverse the list .
domains
x=integer
list=integer*
predicates
append(x,list,list)
rev(list,list)
clauses
append(X,[],[X]).
append(X,[H|T],[H|T1]):-append(X,T,T1).
rev([],[]).
rev([H|T,rev):-rev(T,L),append(H,L,rev).
Output: append(2,[1,2,4],X)
Rev([1,2,3,4],X)
Program to append an integer into the list .
domains
x=integer
list=integer*
predicates
append(x,list,list)
clauses
append(X,[],[X]).
append(X,[H|T],[H|T1]):-
append(X,T,T1).
Program to replace an integer from the list .
domains
list=integer*
predicates
replace(integer,integer,list,list)
clauses
replace(X,Y,[X|T],[Y|T]).
replace(X,Y,[H|T],[H|T1]):-replace(X,Y,T,T1).
Program to delete an integer from the list .
domains
list=integer*
predicates
del(integer,list,list)
clauses
del(X,[X|T],T).
del(X,[H|T],[H|T1]):-
del(X,T,T1).
Program to show concept of list.
domains
name=symbol*
predicates
itnames(name)
clauses
itnames([ram,kapil,shweta]).
itnames([ram,shweta,kapil]).
Program to demonstrate family relationship
predicates
parent(symbol,symbol)
child(symbol,symbol)
mother(symbol,symbol)
brother(symbol,symbol)
sister(symbol,symbol)
grandparent(symbol,symbol)
male(symbol)
female(symbol)
clauses
parent(a,b).
sister(a,c).
male(a).
female(b).
child(X,Y):-parent(Y,X).
mother(X,Y):-female(X),parent(X,Y).
grandparent(X,Y):-parent(X,Z),parent(Z,Y).
brother(X,Y):-male(X),parent(V,X),parent(V,Y).
Steps for 8-queen problem
STEP 1 : Represent the board positions as 8*8 vector , i.e., [1,2,3,4,5,6,7,8]. Store the set
of queens in the list ‘Q’.
STEP 2 : Calculate the permutation of the above eight numbers stored in set P.
STEP 3 : Let the position where the first queen to be placed be (1,Y), for second be
(2,Y1) and so on and store the positions in Q.
STEP 4 : Check for the safety of the queens through the predicate , ‘noattack ()’.
STEP 5 : Calculate Y1-Y and Y-Y1. If both are not equal to Xdist , which is the X –
distance between the first queen and others, then go to Step 6 else go to Step 7.
STEP 6 : Increment Xdist by 1.
STEP 7 : Repeat above for the rest of the queens , until the end of the list is reached .
STEP 8 : Print Q as answer .
STEP 9 : Exit.
Write a program for 8-queen problem
domains
H=integer
T=integer*
predicates
safe(T)
solution(T)
permutation(T,T)
del(H,T,T)
noattack(H,T,H)
clauses
del(I,[I|L],L). /*to take a position from the permutation of list*/
del(I,[F|L],[F|L1]):-
del(I,L,L1).
permutation([],[]). /*to find the possible positions*/
permutation([H|T],PL):-
permutation(T,PT),\
del(H,PL,PT).
solution(Q):- /*final solution is stored in Q*/
permutation([1,2,3,4,5,6,7,8],Q),
safe(Q).
safe([]). /*Q is safe such that no queens attack each other*/
safe([Q|others]):-
safe(others),
noattack(Q,others,1).
noattack(_,[],_). /*to find if the queens are in same row, column or
diagonal*/
noattack(Y,[Y1|Ydist],Xdist):-
Y1-Y<>Xdist,
Y-Y1<>Xdist,
dist1=Xdist,
noattack(Y,Ydist,dist1).