INDEX S.No. Title of experiment 1 Program to find the route distance between two cities 2 Program to implement tower of Hanoi 3 Program to calculate the factorial of a number. 4 Program to implement family relationships 5 Program To Logon Example With Recursion And No Repeat predicate 6 7 8 Program to print the list of customers having different coloured cars with price and model available Program to implement water jug problem 9 Program to implement breadth first search Recursion In PROLOG 10 Queries SIGNATURE PROGRAM NO. – 1 AIM: TO FIND THE ROUTE DISTANCE BETWEEN TWO CITIES DOMAINS Town = symbol Distance = integer PREDICATES nondeterm road(Town,Town,Distance) nondeterm route(Town,Town,Distance) CLAUSES road("Karimnagar","Warangal",100). road("Karimnagar","Hyderabad",120). road("Hyderabad","Warangal",120). road("Warangal","Vijaywada",120). road("Hyderabad","Vijaywada",120). road("Hyderabad","Kazipet",170). road("Kazipet","Vijaywada",220). road("Warangal","Vishakapatnam",170). road("Vijaywada","Vishakapatnam",120). route(Town1,Town2,Distance):road(Town1,Town2,Distance). route(Town1,Town2,Distance):road(Town1,X,Dist1), route(X,Town2,Dist2), Distance=Dist1+Dist2, !. GOAL route("Karimnagar", "Vishakapatnam", X), write("Distance from Karimnagar to Vishakapatnam is ",X),nl. Output: Distance from Karimnagar to Vishakapatnam is 270 X=270 1 Solution PROGRAM NO. – 2 AIM: TO IMPLEMENT TOWER OF HANOI Domains A,B,C,N,Z=INTEGER Predicates Tower(integer,integer,integer,integer). Clauses Tower(1,A,C,B):write("move from",A,"to",C),nl. Tower(N,A,C,B):Z=N-1, Tower(Z,A,B,C), write("move from",A,"to",C),nl, Tower(Z,B,C,A). goal:Tower(3,1,3,2). Output: move move move move move move move yes from1to3 from1to2 from3to2 from1to3 from2to1 from2to3 from1to3 PROGRAM NO. – 3 AIM: TO CALCULATE THE FACTORIAL OF A NUMBER domains A,B,Y,Z=Integer predicates fact(integer,integer) go clauses go:write("enter the no."),nl, readint(A), B=1, fact(A,B). fact(A,B):A<>1, Y=A-1, Z=A*B, fact(Y,Z). fact(1,B):write("factorial is",B). goal:go. Output: enter the no. 6 factorial is720yes PROGRAM NO. – 4 AIM: TO IMPLEMENT FAMILY RELATIONSHIPS DATABASE - tmp son(STRING,STRING) sister(STRING,STRING) brother(STRING,STRING) married(STRING,STRING) CLAUSES son("Deepak","Jack"). sister("Jiya","swati"). brother("Sonia", "Harsh"). married("Jack", "Jiya"). married("Lokesh", "Sonia"). married("Priya","Deepak"). PREDICATES father(STRING father,STRING child) grandfather(STRING grandfather,STRING grandchild) sister_in_law(STRING,STRING) brother_in_law(STRING,STRING) father_in_law(STRING,STRING) CLAUSES father(A,B):son(B,A). grandfather(A,B):-father(A,C), father(C,B). sister_in_law(A,B):-married(A,C), sister(C,B). sister_in_law(A,B):-brother(A,C), married(C,B). brother_in_law(A,B):-married(A,C), brother(C,B). brother_in_law(A,B):-brother(A,C), married(C,B). father_in_law(A,B):-married(A,C), son(C,B). GOAL father_in_law("Priya",Z), format(Msg,"father_in_law(\"Priya\",%)",Z), write(Msg). %GOAL father_in_law("Priya",Z). Output: father_in_law("Priya",Jack)Z=Jack, Msg=father_in_law("Priya",Jack) 1 Solution PROGRAM NO. – 5 AIM: LOGON EXAMPLE WITH RECURSION AND NO REPEAT PREDICATE domains Name,Password = symbol predicates getinput(Name,Password) logon user(Name,Password) clauses logon :getinput(Name,Password), user(Name,Password), write("You are now logged on."),nl. logon :write("Sorry, you are not permitted access."), write("Please try again."),nl, logon. getinput(Name,Password) :-write("Please Enter Your Name:"), readln(Name),nl, write("Please Enter Password:"), readln(Password),nl. user(bill,bigfoot). user(john,superman). user(sue,happy). goal: getinput(Name,Password). Output: Please Enter Your Name:bill Please Enter Password:bigfoot Name=bill, Password=bigfoot 1 Solution PROGRAM NO. – 6 AIM: TO PRINT THE LIST OF CUSTOMERS HAVING DIFFERENT COLORED CARS WITH PRICE AND MODEL AVAILABLE. Domains namelist=names* names=symbol color=symbol list=names* Predicates cust_names(namelist,color) model(color,list) cost(symbol) price(color,integer) Clauses cust_names([jitander,preeti,veena,avinash,jyeshtha],green). cust_names([arvind,poonam,abhijeet],white). cust_names([daya,bhanu,anuradha,anju],red). model(red,[hyundai_accent,ford_monedo,indgo]). model(white,[maruti_esteem,maruti_baleno]). model(green,[toyota_avalon,lotus_elise]). model(green,[toyta_avalon,lotus_elise]). price(red,450000). price(white,350000). price(green,430000). cost(green):cust_names(A,green),nl, write(A),nl, price(green,X),nl, write("the price of green color car is ",X),nl, model(green,Y),nl, write("the model available in green color ",Y),nl. cost(white):cust_names(A,white),nl, write(A),nl, price (white,X),nl, write("the price of white color car is ",X),nl, model(blue,Y),nl, write(" the models available in white color ",Y),nl. goal: write(" name list of customers who own green color car :-"),nl, cost(green). /*write("name list of customers who own red color car :-"),nl, cost(red). write("name list of customer who own white color car :"),nl, cost(white). */ Output: name list of customers who own green color car :["jitender","preeti","veena","avinash","jyeshtha"] the price of green color car is 430000 the model available in green color ["toyota_avalon","lotus_elise"] yes PROGRAM NO. – 7 AIM: TO IMPLEMENT WATER JUG PROBLEM Domains Z,A,X,Y=integer Predicates waterjug(X,Y) Clauses waterjug(X,Y):X=0, Y=0, write("4l jug empty & 3l jug empty"),nl, Z=0, A=3, waterjug(Z,A). waterjug(X,Y):X=0, Y=3, write("4l jug empty & 3ljug 3l water"),nl, Z=3, A=0, waterjug(Z,A). waterjug(X,Y):X=3, Y=0, write(" 4l jug 3l water & 3l jug empty"),nl, Z=3, A=3, waterjug(Z,A). waterjug(X,Y):Z=3, A=3, write("4l jug 3l water& 3l jug 3l water"),nl, Z=4, A=2, waterjug(Z,A). waterjug(X,Y):X=4, Y=2, write("4l jugh 4l water & 3l jug 2l water"),nl, Z=0, A=2, waterjug(Z,A). waterjug(X,A):X=0, Y=2, write("4l jug empty & 3l jug 2l water"),nl, Z=2, A=0, waterjug(Z,A). waterjug(X,Y):X=2, Y=0, write("Goal Achieved"). goal:waterjug(0,0). Output: 4l jug empty & 3l jug empty 4l jug empty & 3ljug 3l water 4l jug 3l water & 3l jug empty 4l jug 3l water& 3l jug 3l water 4l jug 3l water& 3l jug 3l water 4l jug 3l water& 3l jug 3l water 4l jug empty & 3l jug 2l water 4l jug 3l water& 3l jug 3l water 4l jug empty & 3l jug 2l water 4l jug 3l water& 3l jug 3l water Goal Achievedyes PROGRAM NO. –8 AIM: TO IMPLEMENT BREADTH FIRST SEARCH Domains W=Integer* Value=Integer Predicates pop(W,W,Value) append(W,W,W) go checkbfs(W) test(Value,W,W) Clauses pop([Head|Tail],S1,X):X=Head, S1=Tail. append([],LIST1,LIST1). append([X|LIST1],LIST2,[X|LIST3]):append(LIST1,LIST2,LIST3). checkbfs([]):write("STACK NULL"). checkbfs(STACK):write("checkbfs"),nl, pop(STACK,STACK2,X), test(X,STACK2,S3), write(S3),nl, checkbfs(S3). test(A,STACK2,S3):A=5,nl, write(A,"goal found"), S3=[]. test(A,STACK2,S3):write(A),nl, S3=STACK2. go:A=[10],B=[7],C=[6],D=[5],E=[12],F=[11],G=[15], H=[16],I=[17],J=[20], append(I,J,R1), append(G,H,R2), append(E,F,R3), append(R2,R1,R4), append(R3,R4,R5), append(C,D,R6), append(B,R6,R7), append(R7,R5,R8), append(A,R8,R9), ROOT=R9, write("Starting Program\n"),nl, checkbfs(ROOT). Goal:go. Output: Starting Program checkbfs 10 [7,6,5,12,11,15,16,17,20] checkbfs 7 [6,5,12,11,15,16,17,20] checkbfs 6 [5,12,11,15,16,17,20] checkbfs 5goal found[] STACK NULLyes PROGRAM NO. –9 Recursion In PROLOG Consider the following database (Pl file). parent(gaurav,suarav). parent(saurav,sita). parent(sita,gita). parent(gita,rattan). parent(suarav,ram). parent(ram,sunny). ancestor(A,B):-parent(A,B). ancestor(A,B):-parent(C,B),ancestor(A,C). OUTPUT: 1 ?- [ancestor]. to consult or compile th PROLOG source file. 2 ?- ancestor(gaurav,saurav). 3 ?- ancestor(gaurav,rattan). 4 ?- ancestor(rattan,saurav). 5 ?- ancestor(WHO,WHOM). 6 ?- ancestor(WHO,WHOM). PROGRAM NO. –10 Answer following queries 1 ?- [LIST]. To consult or compile the PROLOG source file. 2 ?- [H1|T] = [1,2,3]. 3 ?- [H1,H2|T] = [1,2,3]. 4 ?- [H1,H2,H3|T] = [1,2,3]. 5 ?- [H1,H2,H3,H4|T] = [1,2,3]. OUTPUT: H1 =1 T=2,3 H1=1 H2=2 T=[3] H1=1 H2=2 H3=3 T=[] false