Вы находитесь на странице: 1из 41

Program 1: THE FAMILY PROGRAM. CODE: parent(pam,bob). parent(tom,bob). parent(tom,liz). parent(bob,ann). parent(bob,pat). parent(pat,jim). female(pam). male(tom). male(bob). female(liz).

female(ann). female(pat). male(jim). offspring(Y,X):- parent(X,Y). mother(X,Y):- parent(X,Y), female(X). grandparent(X,Z):- parent(X,Y), parent(Y,Z). sister(X,Y):- parent(Z,X), parent(Z,Y). female(X), different(X,Y). predecessor(X,Z):- parent(X,Z). predecessor(X,Z):- parent(X,Y), predecessor(Y,Z). OUTPUT: 1 ?- predecessor(pam,jim). true . 2 ?- predecessor(pam,X). X = bob ; X = ann ; X = pat ; X = jim ; false.

Program 2: CODE: big(bear). big(elephant). small(cat). brown(bear). black(cat). gray(elephant). dark(Z):- black(Z). dark(Z):- brown(Z). OUTPUT: 1 ?- dark(X),big(X). X = bear.

Program 3: Different VERSION of FAMILY program. CODE: parent(pam,bob). parent(tom,bob). parent(tom,liz). parent(bob,ann). parent(bob,pat). parent(pat,jim). pred1(X,Z):- parent(X,Z). pred1(X,Z):- parent(X,Y), pred1(Y,Z). pred2(X,Z):- parent(X,Y), pred2(Y,Z). pred2(X,Z):- parent(X,Z). pred3(X,Z):- parent(X,Z). pred3(X,Z):- pred3(X,Y), parent(Y,Z). pred4(X,Z):- pred4(X,Y), parent(Y,Z). pred4(X,Z):- parent(X,Z). OUTPUT: 1 ?- pred1(tom,pat). true . 2 ?- pred2(tom,pat). true . 3 ?- pred3(tom,pat). true . 4 ?- pred4(tom,pat). ERROR: Out of local stack Exception: (1,675,218) pred4(tom, _G367) ? creep

Program 4: Define two predicates Evenlength(List) and oddlength(List) so that they are true if their argument is a list of even or odd length respectively. CODE: evenlength(X):- length(X,N),N mod 2=:=0. oddlength(X):- length(X,N),N mod 2=:=1. OUTPUT: 1 ?- evenlength([a,b,c,d]). true. 2 ?- oddlength([a,b,c,d]). false.

Program 5: Define the relation reverse(List,ReversedList) that reverses the list. CODE: reverse([],[]). reverse([First|Rest],Reversed):- reverse(Rest,ReversedRest), conc(RevesredRest,[First],Reversed). conc([],L,L). conc([X|L1],L2,[X|L3]):-conc(L1,L2,L3). OUTPUT: 1 ?- reverse([n,e,h,a],R). R = [a, h, e, n].

Program 6: Define the predicate that palindrome(List). A list is a palindrome if it reads the same in the forward and in the backward direction. CODE: palindrome(List):- reverse(List,List). reverse([],[]). reverse([First|Rest],Reversed):- reverse(Rest,ReversedRest), conc(RevesredRest,[First],Reversed). conc([],L,L). conc([X|L1],L2,[X|L3]):-conc(L1,L2,L3). OUTPUT: 1 ?- palindrome([m,a,d,a,m]). true. 2 ?- palindrome([n,e,h,a]). false.

Program 7: Define the relation shift(List1,List2) so that List2 is List1 shifted rotationally by one element to the left. CODE: shift([First|Rest],Shifted):- conc(Rest,[First],Shifted). conc([],L,L). conc([X|L1],L2,[X|L3]):-conc(L1,L2,L3). OUTPUT: 1 ?- shift([1,2,3,4,5],L1),shift(L1,L2). L1 = [2, 3, 4, 5, 1], L2 = [3, 4, 5, 1, 2].

Program 8: Define relation dividelist(List,List1,List2) so that the elements of the List are partitioned between List1 and List2, and List1 and List2 are approximately the same length. CODE: dividelist([],[],[]). dividelist([X],[X],[]). dividelist([X,Y|List],[X|List1],[Y|List2]):-dividelist(List,List1,List2). OUTPUT: 1 ?- dividelist([a,b,c,d,e],List1,List2). List1 = [a, c, e], List2 = [b, d].

Program 9: Define the relation flatten(List,FlatList) where List can be a list of lists, and FlatList is List flattened so that the elements of Lists sublists are reorganized as one plain list. CODE: flatten([Head|Tail],FlatList):- flatten(Head,FlatHead), flatten(Tail,FlatTail), conc(FlatHead,FlatTail,FlatList). conc([],L,L). conc([X|L1],L2,[X|L3]):-conc(L1,L2,L3). OUTPUT: 1 ?- flatten([a,b,[c,d],[],[[[e]]],f],L). L = [a, b, c, d, e, f].

Program 10: Define the relation max(X,Y,Max) so that Max is greater of the two X and Y. CODE: max(X,Y,X):- X>=Y. max(X,Y,Y):- X<Y. OUTPUT: 1 ?- max(2,5,Max). Max = 5.

Program 11: Define the predicate maxlist(List,Max) so that Max is the greatest number in the list of numbers List. CODE: maxlist([X],X). maxlist([X,Y|Tail],Max):- maxlist([Y|Tail],Max1), max(X,Max1,Max). max(X,Y,X):- X>=Y. max(X,Y,Y):- X<Y. OUTPUT: 1 ?- maxlist([3,9,4,5,12],Max). Max = 12.

Program 12: Define the predicate sumlist(List,Sum) so that the Sum is the sum of the given list of numbers List. CODE: sumlist([],0). sumlist([Head|Tail],Sum):- sumlist(Tal,Sum1), Sum is Head + Sum1. OUTPUT: 1 ?- sumlist([1,2,6,4,21],Sum). Sum = 34.

Program 13: Define the predicate ordered(List) which is true if List is an ordered list of numbers. CODE: ordered([X]). ordered([X,Y|Tail]):- X=<Y, ordered([Y|Tail]). OUTPUT: 1 ?- ordered([1,5,6,6,9,12]). true . 2 ?- ordered([1,7,3,8,5]). false.

Program 14: Define the predicate subsum(Set,Sum,SubSet) so that Set is a list of numbers, SubSet is a subset of these numbers,a nd the sum of the numbers im SubSet is Sum. CODE: subsum([],0,[]). subsum([N|List],Sum,[N|Sub]):- Sum1 is Sum-N, subsum(List,Sum1,Sub). % N is in subset subsum([N|List],Sum,Sub):- subsum(List,Sum,Sub). % N is notin subset OUTPUT: 1 ?- subsum([1,2,5,3,2],5,Sub). Sub = [1, 2, 2] ; Sub = [2, 3] ; Sub = [5] ; Sub = [3, 2] ; false.

Program 15: Define the procedure between(N1,N2,X) which, for two given integers N1 and N2, generates through backtracking all the inetegers X that satisfy the constraint N1<= X<=N2. CODE: between(N1,N2,N1):- N1=< N2. between(N1,N2,X):- N1<N2, NewN1 is N1+1, between(NewN1,N2,X). OUTPUT: 30 ?- between(4,9,X). X=4; X=5; X=6; X=7; X=8; X=9; false.

Program 16: Define the predicate length(List,N) where N is the length of the given list. CODE: length([],0). length([_|Tail],N):- length(Tail,N1), N is 1+N1. OUTPUT: 1 ?- length([a,b,[c,d],e],N). N = 4.

Program 17: Define the predicate gcd(X,Y,D) so that D is the greatest common divisor of two numbers X and Y. CODE: gcd(X,X,X). gcd(X,Y,D):- X<Y, Y1 is Y-X, gcd(X,Y1,D). gcd(X,Y,D):- Y<X, gcd(Y,X,D). OUTPUT: 1 ?- gcd(20,25,D). D=5. 2 ?- gcd(3,6,D). D=3.

Program 18: Define the predicate Permutation(List,P) so that P generates all the permutations of the given list. CODE: a) permutation([],[]). permutation([X|L],P):- permutation(L,L1), insert(X,L1,P). insert(X,L1,P):- del(X,P,L1). del(X,[X|Tail],Tail). del(X,[Y|Tail],[Y|Tail1]):- del(X,Tail,Tail1). OUTPUT: 1 ?- permutation([a,b,c],P). P = [a, b, c] ; P = [b, a, c] ; P = [b, c, a] ; P = [a, c, b] ; P = [c, a, b] ; P = [c, b, a] ; false.

b) permutation([],[]). permutation(L,[X|P]):- del(X,L,L1), permutation(L1,P). del(X,[X|Tail],Tail). del(X,[Y|Tail],[Y|Tail1]):- del(X,Tail,Tail1). OUTPUT: 1 ?- permutation([a,b,c],P). P = [a, b, c] ; P = [a, c, b] ; P = [b, a, c] ; P = [b, c, a] ; P = [c, a, b] ; P = [c, b, a] ; false.

Program 19: Define the predicate sublist(S,L) so that S occurs within L as its sublist. CODE: sublist(S,L):- conc(L1,L2,L), conc(S,L3,L2). conc([],L,L). conc([X|L1],L2,[X|L3]):- conc(L1,L2,L3). OUTPUT: 1 ?- sublist(S,[a,b,c]). S = [] ; S = [a] ; S = [a, b] ; S = [a, b, c] ; S = [] ; S = [b] ; S = [b, c] ; S = [] ; S = [c] ; S = [] ; false.

Program 20: Define the predicate member(X,L) so that it return true if X occurs in list L. CODE: member(X,[X|Tail]). member(X,[Head|Tail]):- member(X,Tail). OUTPUT: 1 ?- member(b,[a,b,c]). true . 2 ?- member(b,[a,[b,c]]). false.

Program 21: Define the predicate Del(X,L,L1) so that L1 is the list returned after deleting element X from list L. CODE: del(X,[X|Tail],Tail). del(X,[Y|Tail],[Y|Tail1]):- del(X,Tail,Tail1). OUTPUT: 1 ?- del(a,[a,b,a,a],L). L = [b, a, a] .

Program 22: Let the program be : p(1). p(2):-!. p(3). Write all Prologs answers to the following questions: CODE: 1 ?- p(X). X=1; X = 2. 2 ?- p(X),p(Y). X = Y, Y = 1 ; X = 1, Y=2; X = 2, Y=1; X = Y, Y = 2. 3 ?- p(X),!,p(Y). X = Y, Y = 1 ; X = 1, Y = 2.

Program 23: The following relation classifies numbers into three classes: positive, zero and negative : class(Number,positive):- Number > 0. class(0,zero). class(Number,negative):- Number < 0. Define this procedure using cuts. CODE: class(Number,positive):- Number > 0,!. class(0,zero):-!. class(Number,negative). OUTPUT: 1 ?- class(2,P). P = positive. 2 ?- class(-6,P). P = negative. 3 ?- class(0,P). P = zero.

Program 24: Define the procedure Split(Numbers,Positives,Negatives) Which splits a list of numbers into two lists: positive ones(including zero) and negatives ones. a) Without using cut. CODE: split([],[],[]). split([X|L],[X|L1],L2):- X>=0, split(L,L1,L2). split([X|L],L1,[X|L2]):- split(L,L1,L2). OUTPUT: 1 ?- split([3,-1,0,5,-2],P,N). P = [3, 0, 5], N = [-1, -2].

b) Using cut. CODE: split([],[],[]). split([X|L],[X|L1],L2):- X>=0,!, split(L,L1,L2). split([X|L],L1,[X|L2]):- split(L,L1,L2). OUTPUT: 1 ?- split([3,-1,0,5,-2],P,N). P = [3, 0, 5], N = [-1, -2].

Program 25: Example demonstrating the CUT. CODE: beat(tom,jim). beat(ann,tom). beat(pam,jim). class(X,fighter):- beat(X,_), beat(_,X),!. class(X,winner):- beat(X,_),!. class(X,sportsman):- beat(_,X). OUTPUT: 1 ?- class(tom,C). C = fighter. 2 ?- class(tom,sportsman). true.

Program 26: Example demonstrating the NEGATION. CODE: beat(tom,jim). beat(ann,tom). beat(pam,jim). class(X,fighter):- beat(X,_), beat(_,X). class(X,winner):- beat(X,_), not beat(_,X). class(X,fighter):- beat(X,_), not beat(_,X). OUTPUT: 1 ?- class(tom,sportsman). false. 2 ?- class(tom,C). C = fighter.

Program 27: Define the procedure count(A,L,N) where A is atom, L is the list and N is the number of occurrences. CODE: count(_,[],0). count(A,[B|L],N):- atom(B),A=B,!, count(A,L,N1), N is N1+1 ; count(A,L,N). OUTPUT: 1 ?- count(a,[a,b,a,a],N). N = 3.

Program 28: Define the predicate conc(L1,L2,L3) that append two list into a third list L3. CODE: conc([],L,L). conc([X|L1],L2,[X|L3]):- conc(L1,L2,L3). OUTPUT: 1 ?- conc([1,2,3],[a,b,c,d],L3). L3 = [1, 2, 3, a, b, c, d].

Program 29: Define the predicate fib(N,R) that returns the nth term of a Fibonacci series. CODE: fib(1,1). fib(2,1). fib(N,R):- N>=3, N1 is N-1, N2 is N-2, fib(N1,R1), fib(N2,R2), R is R1+R2. OUTPUT: 1 ?- fib(8,R). R = 21.

Program 30: Define the predicate bagof(X,P,L) that produce the list L of all the object X such that a goal P is satisfied. CODE: age(peter,7). age(ann,5). age(pat,8). age(tom,5). OUTPUT: 1 ?- bagof(Child,age(Child,Age),List). Age = 5, List = [ann, tom] ; Age = 7, List = [peter] ; Age = 8, List = [pat].

Program 31: Define the predicate setof(X,P,L) that produce the list L of object X such that a goal P is satisfied. CODE: age(peter,7). age(ann,5). age(pat,8). age(tom,5). OUTPUT: 1 ?- setof(Child,Age^age(Child,Age),ChildList), | setof(Age,Child^age(Child,Age),AgeList). ChildList = [ann, pat, peter, tom], AgeList = [5, 7, 8].

Program 32: Define the predicate findall(X,P,L) that produce the list L objects that goal P is satisfied. CODE: age(peter,7). age(ann,5). age(pat,8). age(tom,5). OUTPUT: 1 ?- findall(Child,age(Child,Age),List). List = [peter, ann, pat, tom].

Program 33: Define the predicate assert(X) such that a clause X is always added to the database. CODE: fast(ann). slow(tom). slow(pat). OUTPUT: 1 ?- assert( | (faster(X,Y):- fast(X),slow(Y))). true. 2 ?- faster(A,B). A = ann, B = tom ; A = ann, B = pat ; A = ann, B = tom ; A = ann, B = pat ; A = ann, B = tom ; A = ann, B = pat. 3 ?- retract(slow(X)). X = tom ; X = pat. 4 ?- faster(ann,_). false.

Program 34: Define the predicate nth(N,L,R)) such that R is the nth element of the list L. CODE: nth(0,[X|_],X). nth(N,[_|T],R):- N1 is N-1, nth(N1,T,R). OUTPUT: 1 ?- nth(4,[1,4,7,2,9,8],R). R=9.

Program 35: Define the predicate pow(N,K,R) such that R is the kth power of a number N. CODE: power(N,0,1). power(N,K,R):- K1 is K-1, power(N,K1,R1), R is R1*N. OUTPUT: 1 ?- power(3,2,R). R=9.

Program 36: Define the predicate to solve problem of tower of Hanoi CODE: hanoi(N):- move(N,left,centre,right). move(1,A,_,C):- inform(A,C),!. move(1,A,B,_):- inform(A,B),!. move(N,A,B,C):- N1 is N-1, move(N1,A,C,B), inform(A,C), move(N1,B,A,C). inform(Location1,Location2):- write('\n move a disk from '), write(Location1), write(' to '), write(Location2). OUTPUT: 1 ?- hanoi(3). move a disk from left to right move a disk from left to centre move a disk from right to centre move a disk from left to right move a disk from centre to left move a disk from centre to right move a disk from left to right true.

Program 37: Define the predicate fact(X,Y) such that Y is the factorial of X. CODE: start(1):-!. start(0):- write('\n Enter any no. : '), read(X), fact(X,Y), write('Factorial of '), write(X), write(' is '), write(Y), write('\nContinue?? 0 for YES, 1 for NO : '), read(Choice), start(Choice). fact(0,1):- !. fact(X,Y):- X1 is X-1, fact(X1,Y1), Y is X*Y1. OUTPUT: 1 ?- start(0). Enter any no. : 6 |: . Factorial of 6 is 720 Continue?? 0 for YES, 1 for NO : 0 |: . Enter any no. : 4 |: . Factorial of 4 is 24 Continue?? 0 for YES, 1 for NO : 1 |: . true.

Program 38: Given a graph, define a predicate to find path between two node of the graph.

CODE: edge(a,b). edge(b,c). edge(c,d). edge(d,e). edge(e,f). edge(a,f). edge(b,e). edge(c,f). edge(b,f). edge(c,e). path(X,Y):- edge(X,Y). path(X,Y):- edge(X,Z),edge(Z,Y). OUTPUT: 1 ?- path(a,b). true . 2 ?- path(a,f). true .

Program 39: Define the predicate mul(X,N,M) to multiply the given no. X with no N. CODE: mul(X,1,X). mul(X,Y,Z):- Y>0, Y1 is Y-1, mul(X,Y1,Z1), Z is X+Z1. OUTPUT: 1 ?- mul(2,8,M). M = 16 .

Program 40: Define the predicate to sort a list using insertion sort. CODE: insertsort([],[]). insertsort([X|T],Sorted):- insertsort(T,SortT), insert(X,SortT,Sorted). insert(X,[Y|Sorted],[Y|Sorted1]):- gt(X,Y),!, insert(X,Sorted,Sorted1). insert(X,Sorted,[X|Sorted]). gt(X,Y):- X>Y. OUTPUT: 1 ?- insertsort([1,4,6,2,3],X). X = [1, 2, 3, 4, 6].

Program 45: Define the predicate merge(L1,L2,L3) that merges two list into a third list L3. CODE: merge([],List,List) :!. merge(List,[],List). merge([X|Rest1],[Y|Rest2],[X|Rest3]) :X<Y,!, merge(Rest1,[Y|Rest2],Rest3). merge(List1,[Y|Rest2],[Y|Rest3]) :merge(List1,Rest2,Rest3). OUTPUT: 1 ?- merge([a,b,c],[x,y,z],L). L = [a, b, c, x, y, z]. 2 ?- merge([4,5,6],[1,2,3],L). L = [1, 2, 3, 4, 5, 6].