Anda di halaman 1dari 45

PRACTICAL-1

8th I.T.

En.No.:080240116057

Aim: Write a Turbo PROLOG program for Family Relationship. domains person = symbol

predicates male(person) female(person) father(person,person) mother(person,person) sister(person,person) parent(person,person) brother(person,person) uncle(person,person) grandfather(person,person)

clauses male(alan). male(charles). male(bob). male(ivan).

female(beverly). female(fay).

8th I.T.

En.No.:080240116057

female(marilyn). female(sally).

mother(marilyn,beverly). mother(alan,sally).

father(alan,bob). father(beverly,charles). father(fay,bob). father(marilyn,alan).

parent(X,Y) if mother(X,Y). parent(X,Y) if father(X,Y).

brother(X,Y) if male(Y) and

/*The brother of X is Y if */ /*Y is a male and */

parent(X,P) and /*the parent of X is P and */ parent(Y,P) and /*the parent of Y is P and */ X <> Y. /* X and Y are not the same */

sister(X,Y) if female(Y) and

/*The sister of X is Y if */ /*Y is female and */

parent(X,P) and /*the parent of X is P and */

8th I.T.

En.No.:080240116057

parent(Y,P) and /*the parent of Y is P and */ X <> Y. /*X and Y are not the same */

uncle(X,U) if

/*The uncle of X is U if */

mother(X,P) and /*the mother of X is P and */ brother(P,U). uncle(X,U) if /*the brother of P is U. */ /*The uncle of X is U if */

father(X,P) and /*the father of X is P and */ brother(P,U). /*the brother of P is U */

grandfather(X,G) if /*The grandfather of X is G */ father(P,G) and /*if the father of P is G */ mother(X,P). /*and the mother of X is P. */

grandfather(X,G) if /*The grandfather of X is G */ father(X,P) and /*if the father of X is P */ father(P,G). /*the father of P is G */

8th I.T.

En.No.:080240116057

PRACTICAL-2

8th I.T.

En.No.:080240116057

Aim: Write a Turbo PROLOG program that list four address in a label form, each address should list a name, one-line address, city, state &ZIP code.

predicates reference(symbol,symbol) goal write("Please type a name :"), readln(The_Name), reference(The_Name,Address,City,State,Zipcode), write("The Address is ",Address). write("The City is ",City). write("The State is ",State). write("The Zipcode is ",Zipcode). clauses reference("Albert","df", "hmt","guj","231565"). reference("Betty", "ew", "abd","guj","458979"). reference("Carol", "sdf", "surat","guj","6789"). reference("Dorothy","sf","nd","guj","488979").

8th I.T.

En.No.:080240116057

PRACTICAL-3

8th I.T.

En.No.:080240116057

Aim: Write a Turbo PROLOG program for diagnosis the childhood diseases. domains disease,indication,name = symbol predicates hypothesis(name,disease) symptom(name,indication) clauses symptom(charlie,fever). symptom(charlie,rash). symptom(charlie,headache). symptom(charlie,runny_nose). hypothesis(patient,measles):symptom(patient,fever), symptom(patient,cough), symptom(patient,conjunctivitis), symptom(patient,runny_nose), symptom(patient,rash).

hypothesis(patient,german_measles):symptom(patient,fever), symptom(patient,headache), symptom(patient,runny_nose),

8th I.T.

En.No.:080240116057

symptom(patient,rash).

hypothesis(patient,flu):symptom(patient,fever), symptom(patient,headache), symptom(patient,bodyache), symptom(patient,conjunctivitis), symptom(patient,chills), symptom(patient,sore_throat), symptom(patient,cough), symptom(patient,runny_nose).

hypothesis(patient,common_cold):symptom(patient,headache), symptom(patient,snezzing), symptom(patient,sore_throat), symptom(patient,chills), symptom(patient,runny_nose).

hypothesis(patient,mumps):symptom(patient,fever), symptom(patient,swollen_glands).

8th I.T.

En.No.:080240116057

hypothesis(patient,chicken_pox):symptom(patient,fever), symptom(patient,rash), symptom(patient,bodyache), symptom(patient,chills).

hypothesis(patient,whooping_cough):symptom(patient,cough), symptom(patient,snezzing), symptom(patient,runny_nose).

8th I.T.

En.No.:080240116057

PRACTICAL-4

8th I.T.

En.No.:080240116057

Aim: Write a Turbo PROLOG program to calculate the roots of quadratic equation Consider all possibilities real, equal,imaginary. predicates solve(real,real,real) reply(real,real,real) mysqrt(real,real,real) equal(real,real) clauses solve(A,B,C) :D = B*B-4*A*C, reply(A,B,D), nl. reply(_,_,D) :- D < 0, write("No solution"), !. reply(A,B,D) :D = 0, X=-B/(2*A), write("x=",X), !. reply(A,B,D) :mysqrt(D,D,SqrtD), X1 = (-B + SqrtD)/(2*A), X2 = (-B - SqrtD)/(2*A), write("x1 = ",X1," and x2 = ",X2).

mysqrt(X,Guess,Root) :NewGuess = Guess-(Guess*Guess-X)/2/Guess, not(equal(NewGuess,Guess)),!, mysqrt(X,NewGuess,Root).

8th I.T.

En.No.:080240116057

mysqrt(_,Guess,Guess).

equal(X,Y) :X/Y > 0.99999 , X/Y < 1.00001.

8th I.T.

En.No.:080240116057

PRACTICAL-5

8th I.T.

En.No.:080240116057

(A)- Aim: Write a Turbo PROLOG program based on simple list. domains namelist=symbol* predicates member(symbol,namelist) club_member(symbol) clauses member(X,[X|_]). member(X,[_|Tail]):member(X,Tail). club_member(Name):member(Name,[alfred,susan,foram,bill,frank,mary]).

(B)- Aim: Write a Turbo PROLOG program based on Writing list.

domains list=symbol* predicates writelist(list) clauses writelist([]). writelist([Head|Tail]):write(Head),nl,writelist(Tail).


8th I.T. En.No.:080240116057

(C)-Aim: Write a Turbo PROLOG program based on Reversing a list.

domains list=integer * predicates reverse_list(list,list) reverse(list,list,list) clauses reverse_list(Inputlist,Outputlist):reverse(Inputlist,[],Outputlist). reverse([],Inputlist,Inputlist). reverse([Head|Tail],List1,List2):reverse(Tail,[Head|List1],List2).

(D)-Aim: Write a Turbo PROLOG program based on last Element of a list. domains list =integer * predicates last_ele(list,list) clauses last_ele([Head],X):-

8th I.T.

En.No.:080240116057

X=[Head]. last_ele([_|Tail],X):last_ele(Tail,X).

(E)-Aim: Write a Turbo PROLOG program based on nth Element of a list. domains list =integer * predicates n_element() clauses n_element([Head|_],1,X):X=Head. n_element([_|Tail],N,X):n_element([Head|Tail],N,X):NN=N-1 n_element(Tail,NN,X).

8th I.T.

En.No.:080240116057

PRACTICAL-6

8th I.T.

En.No.:080240116057

Aim: Write a Turbo PROLOG program Checking for Password. domains name,password=symbol predicates getentry(name,password) logon(integer) user(name,password) go clauses go:logon(3), write("u r now logon:"),nl. logon(0):-!, write("sorry u r not permitted access:"), fail. logon(_):getentry(_,_). logon(X):write("illegal entry:"),nl, XX=X-1, logon(XX). getentry(Name,Password):8th I.T. En.No.:080240116057

write("please enter yr name:"), readln(Name),nl, write("enter password:"),nl. readln(Password),nl, user(Name,Password). user(bill,bigfoot).

8th I.T.

En.No.:080240116057

PRACTICAL-7

8th I.T.

En.No.:080240116057

(A)-Aim: Write a Turbo PROLOG program based on Factorial. domains n, f = real predicates factorial(n,f) clauses factorial(1,1). factorial(N,Res) if N > 0 and N1 = N-1 and factorial(N1,FacN1) and Res = N*FacN1.

8th I.T.

En.No.:080240116057

(B)-Aim: Write a Turbo PROLOG program based on Fibonaci series.

predicates go fibo(integer,integer,integer)

clauses go:write("enter no="), readint(X), fibo(0,1,X).

fibo(_,_,1):write("1"),nl,write("1").

fibo(A,B,C):B<C, write(B),nl, D=A+B, fibo(B,D,C).

8th I.T.

En.No.:080240116057

PRACTICAL-8

8th I.T.

En.No.:080240116057

Aim: Write a program to find Minimum and Maximum from give Numbers. domains predicates max(integer,integer,integer) min(integer,integer,integer) maxmin(integer,integer,integer) clauses max(A,B,C):A>B, A>C, write(A). max(A,B,C):A>B, write(C). max(_,B,C):B>C, write(B). max(_,_,C):write(C).

8th I.T.

En.No.:080240116057

min(A,B,C):A<B, A<C, write(A). min(A,B,C):A<B, write(C). min(_,B,C):B<C, write(B). min(_,_,C):write(C).

maxmin(A,B,C):max(A,B,C), min(A,B,C).

8th I.T.

En.No.:080240116057

PRACTICAL-9

8th I.T.

En.No.:080240116057

Aim: Write a Turbo PROLOG program to implement Tower Of Hanoi Problem. domains loc = right ; middle ; left predicates hanoi(integer) move(integer,loc,loc,loc) inform(loc,loc) clauses hanoi(N) :- move(N,left,middle,right).

move(1,A,_,C) :- inform(A,C),!. move(N,A,B,C) :N1=N-1,move(N1,A,C,B),inform(A,C),move(N1,B,A,C).

inform(Loc1,Loc2):write("\nMove a disk from ",Loc1," to ",Loc2).

8th I.T.

En.No.:080240116057

PRACTICAL-10

8th I.T.

En.No.:080240116057

Aim: Write a Turbo PROLOG program for finding the average salary of an employee and for adding and deleting employees from the database.

8th I.T.

En.No.:080240116057

PRACTICAL-11

8th I.T.

En.No.:080240116057

Aim: Write a Turbo PROLOG program to solve Water-Jug Problem. domains j1=integer j2=integer predicates water(j1,j2) water(integer,integer,integer,integer) clauses water(2,B,_,_):-B<4. water(A,_):-A>4,write("Jug One capacity exceeded"). water(_,B):-B>3,write("Jug two capacity exceeded"). water(A,B):-A=4,B=0,X=1,Y=3,write("jug1=",X," ","jug2=",Y). water(P,Q):-P=1,Q=3,P1=1,Q1=0,write("jug1=",P1," ","jug2=",Q1). water(P,Q):-P=1,Q=0,P1=0,Q1=1,write("jug1=",P1," ","jug2=",Q1). water(P,Q):-P=0,Q=1,P1=4,Q1=1,write("jug1=",P1," ","jug2=",Q1). water(P,Q):-P=4,Q=1,P1=2,Q1=3,write("jug1=",P1," ,"jug2=",Q1), write("Solution reached").

8th I.T.

En.No.:080240116057

PRACTICAL-12

8th I.T.

En.No.:080240116057

Aim: Write a Turbo PROLOG program to demonstrate the effective use of Cut .

predicates location(string,string) go chkstate(string) clauses go:writef("%-10 %5 \n","city","state"), fail. gp:location(city,state), writef("%-10 %2 \n",city,state), fail. go. location("Jackson","MS"). location("washington","DC"). location("Raleigh","NC"). chkstate("DC"):!,fail. chkstate(_).

8th I.T.

En.No.:080240116057

Aim: Write a Turbo PROLOG program to demonstrate the effective use of Fail. predicates location(string,string) go clauses go:location(city,state), writef("%-10 %2 \n",city,state), fail. go. location("Jackson","MS"). location("washington","DC"). location("Raleigh","NC").

8th I.T.

En.No.:080240116057

PRACTICAL-13

8th I.T.

En.No.:080240116057

Aim: Write a Turbo PROLOG program for traveling salesman problem.

Domains town = symbol distance = unsigned rib = r(town,town,distance) tlist = town* rlist = rib* predicates nondeterm way(town,town,rlist,distance) nondeterm route(town,town,rlist,tlist,distance) nondeterm route1(town,tlist,rlist,tlist,distance) nondeterm ribsmember(rib,rlist) nondeterm townsmember(town,tlist) nondeterm tsp(town,town,tlist,rlist,tlist,distance) nondeterm ham(town,town,tlist,rlist,tlist,distance) nondeterm shorterRouteExists(town,town,tlist,rlist,distance) nondeterm alltown(tlist,tlist) nondeterm write_list(tlist)

8th I.T.

En.No.:080240116057

clauses write_list([]). write_list([H|T]):write(H,' '), write_list(T). townsmember(X,[X|_]). townsmember(X,[_|L]):townsmember(X,L). ribsmember(r(X,Y,D),[r(X,Y,D)|_]). ribsmember(X,[_|L]):ribsmember(X,L). alltown(_,[]). alltown(Route,[H|T]):townsmember(H,Route), alltown(Route,T). way(Town1,Town2,Ways,OutWayDistance):ribsmember(r(Town1,Town2,D),Ways), OutWayDistance = D. %/* way(Town1,Town2,Ways,OutWayDistance):ribsmember(r(Town2,Town1,D),Ways), OutWayDistance = D.
8th I.T.

En.No.:080240116057

%*/ route(Town1,Town2,Ways,OutRoute,OutDistance):route1(Town1,[Town2],Ways,OutRoute,T1T2Distance), %SWITCH HERE way(Town2,Town1,Ways,LasDist), OutDistance = T1T2Distance + LasDist. route1(Town1,[Town1|Route1],_,[Town1|Route1],OutDistance):OutDistance = 0. route1(Town1,[Town2|PassedRoute],Ways,OutRoute,OutDistance):way(TownX,Town2,Ways,WayDistance), not(townsmember(TownX,PassedRoute)), route1(Town1,[TownX,Town2|PassedRoute],Ways,OutRoute,Completin gRoadDistance), OutDistance = CompletingRoadDistance + WayDistance. shorterRouteExists(Town1,Town2,Towns,Ways,Distance):ham(Town1,Town2,Towns,Ways,_,Other), Other < Distance. tsp(Town1,Town1,Towns,Ways,BestRoute,MinDistance):way(OtherTown,Town1,Ways,_), tsp(Town1,OtherTown,Towns,Ways,BestRoute,MinDistance). tsp(Town1,Town2,Towns,Ways,BestRoute,MinDistance):ham(Town1,Town2,Towns,Ways,Route,Distance),
8th I.T. En.No.:080240116057

not(shorterRouteExists(Town1,Town2,Towns,Ways,Distance)), BestRoute = Route, MinDistance = Distance. ham(Town1,Town2,Towns,Ways,Route,Distance):route(Town1,Town2,Ways,Route,Distance), %SWITCH HERE alltown(Route,Towns), % if you want simple road without including all towns you could uncomment this line write_list(Route), write(" \tD = ",Distance,"\n"). % fail.

8th I.T.

En.No.:080240116057

PRACTICAL-14

8th I.T.

En.No.:080240116057

Aim: Write a Turbo PROLOG program for monkey banana problem.


domains slist=string* predicates member(string,slist) check(string) monkey(string,string,string) check1(char) check2(char) clauses monkey(C,B,S):L=["chair","stick","banana"],member(C,L),member(B,L),member(S,L),check(C),c heck(B),check(S),nl,write("Climb the chair"),nl,write("Take the bananas"),nl. check(X):-X="chair",nl,write("Is chair under bananas?(Y/N) "),readchar(CH),check1(CH). check(X):-X="stick",nl,write("Is stick in hand? (Y/N) "),readchar(CH1),check2(CH1). check(X):-X="banana". check1(G):-G='N',write("Place chair under bananas"),nl. check1(G):-G='Y'. check2(H):-H='N',write("Take stick in hand"). check2(H):-H='Y'. member(P,[P_]). member(P,[_T]):-member(P,T).

8th I.T.

En.No.:080240116057

Practical 15

8th I.T.

En.No.:080240116057

Aim: Write a Turbo PROLOG program N-QUEEN problem.

domains queen = q(integer,integer) queens=queen* freelist = integer* board=board(queens,freelist,freelist,freelist,freelist) predicates placeN(integer,board,board) place_a_queen(integer,board,board) nqueens(integer) makelist(integer,freelist) findandremove(integer,freelist,freelist) clauses nqueens(N):makelist(N,L),Diagonal=N*2-1,makelist(Diagonal,LL), placeN(N,board([],L,L,LL,LL),Final),write(Final).

placeN(_,board(D,[],[],D1,D2),board(D,[],[],D1,D2)):-!. placeN(N,Board1,Result):place_a_queen(N,Board1,Board2),

8th I.T.

En.No.:080240116057

placeN(N,Board2,Result).

place_a_queen(N,board(Queens,Rows,Columns,Diag1,Diag2), board([q(R,C)|Queens],NewR,NewC,NewD1,NewD2)):findandremove(R,Rows,NewR), findandremove(C,Columns,NewC), D1=N+C-R,findandremove(D1,Diag1,NewD1), D2=R+C-1,findandremove(D2,Diag2,NewD2).

findandremove(X,[X|Rest],Rest). findandremove(X,[Y|Rest],[Y|Tail]):findandremove(X,Rest,Tail).

makelist(1,[1]). makelist(N,[N|Rest]):N>0,N1=N-1,makelist(N1,Rest).

8th I.T.

En.No.:080240116057