1.WRITE A PROGRAM TO FIND OUT GRANDPARENT AND SIBLING OF A PARTICULAR MEMBER FROM A GIVEN FAMILY TREE
SOURCE CODE
parent( pam, bob).
parent( tom, bob).
parent( tom, liz).
parent( bob, ann).
parent( bob, pat).
parent( pat, jim).
grandparent(X,Z):- parent(X,Y),parent(Y,Z).
sibling(X,Y):- parent(X,Z),parent(Y,Z).
OUTPUT
% c:/users/bum/documents/prolog/parent compiled 0.00 sec, 9 clauses
1 ?-
| grandparent(X,ann).
X = pam
2.CONCAT A NUMBER AT THE BEGINNING OF A LIST
SOURCE CODE
conc([],L,L).
conc([X|L1],L2,[X|L3]):-conc(L1,L2,L3).
OUTPUT
% c:/users/bum/documents/prolog/beg compiled 0.00 sec, 3 clauses
2 ?- conc([10],[1,2,3,4],R).
R = [10, 1, 2, 3, 4].
3.DELETE A NUMBER FROM THE BEGINNING OF A LIST
SOURCE CODE
del(X,[X|T],T).
del(X,[Y|T],[Y|T1]):-del(X,T,T1).
OUTPUT
% c:/users/bum/documents/prolog/delbegining compiled 0.00 sec, 3 clauses
3 ?-
| del(1,[1,2,3,4,5],R).
R = [2, 3, 4, 5]
4.WRITE A PROGRAM TO FIND OUT LENGTH OF A LIST
SOURCE CODE:
length([],0).
length([H|T],N):-length(T,N1),N is N1 + 1.
OUTPUT
% c:/users/bum/documents/prolog/length compiled 0.02 sec, 58 clauses
.
4 ?- length([2,6,3,9],N).
N = 4.
5.Prolog program to find last item of the list
SOURCE CODE
domains
namelist = symbol*
predicates
lastd(namelist,symbol)
clauses
lastd([Head],X):-
X = Head.
lastd([_|Tail],X):-
lastd(Tail,X).
OUTPUT
% c:/users/bum/documents/prolog/lastlist compiled 0.00 sec, 3 clauses
2 ?-
| lastd([a,b,c,d],X).
X = d
6. PROLOG PROGRAM TO DETERMINE WHETHER A ELEMENT IS A MEMBER OF LIST
SOURCE CODE
trace
domains
namelist = symbol*
N = symbol
predicates
member(symbol,namelist)
delete(symbol,namelist,namelist)
clauses
member(X,List):-
delete(X,List,_).
delete(X,[X|Tail],Tail).
delete(X,[Y|Tail1],[Y|Tail2]):-
delete(X,Tail1,Tail2).
OUTPUT
% c:/users/bum/documents/prolog/elementbelong compiled 0.02 sec, 4 clauses
7 ?-
| member(x,[a,b,x,z,y]).
True
7.Prolog program to concatenate two lists giving third list
SOURCE CODE
domains
list=symbol*
predicates
con(list,list,list)
clauses
con([],L1,L1).
con([X|Tail],L2,[X|Tail1]):-
con(Tail,L2,Tail1).
OUTPUT
% c:/users/bum/documents/prolog/append compiled 0.00 sec, 3 clauses
.8 ?- con([a,b,c],[d,e],ConcatList).
ConcatList = [a, b, c, d, e].
8.Prolog program to append list to generate third list.
SOURCE CODE
con([],L1,L1).
con([X|Tail],L2,[X|Tail1]):-
con(Tail,L2,Tail1).
OUTPUT
% c:/users/bum/documents/prolog/append compiled 0.00 sec, 3 clauses
5 ?-
| con([a,b,c],[d,e],ConcatList).
ConcatList = [a, b, c, d, e].
9.Prolog Program to find GCD of two positive numbers
SOURCE CODE
Code:
gcd1(X,X,X).
gcd1(X,Y,Z) :- X>Y,D is X-Y,gcd1(D,Y,Z).
gcd1(X,Y,Z) :- X<Y,D is Y-X,gcd1(X,D,Z).
OUTPUT
% c:/users/bum/documents/prolog/gcd compiled 0.00 sec, 4 clauses
9 ?-
| gcd1(45,25,Z),write(Z).
5
Z = 5
10.Prolog program to reverse a list
Source code
domains
list=integer*
predicates
reverse_list(list,list)
reverse(list,list,list)
clauses
reverse_list(Inputlist,Outputlist):-
reverse(Inputlist,[],Outputlist).
reverse([],Outputlist,Outputlist).
reverse([Head|Tail],List1,List2):-
reverse(Tail,[Head|List1],List2).
OUTPUT
% c:/users/bum/documents/prolog/reverse compiled 0.00 sec, 4 clauses
13 ?-
| reverse_list([1,2,3],X).
X = [3, 2, 1].
11.Prolog program to find factorial
SOURCE CODE
factorial(0,X):-
X=1.
X=1.
/* recursion for factorial */
factorial(N,X):-
NN=N-1,
NN=N-1,
factorial(NN,X1),
X=X1*N.
/*One argument function*/
factorial(N):-
factorial(N,X),
factorial(N,X),
write(X).
OUTPUT
% c:/users/bum/documents/prolog/fact compiled 0.00 sec, 4 clauses
2 ?-
| factorial(5,X).
120
12.PROLOG PROGRAM TO FIND OUT MAXIMUM OF TWO NUMBERS
SOURCE CODE
max(A,B,C):-A>B,C is A.
max(A,B,C):-B>A,C is B.
OUTPUT
c:/users/bum/documents/prolog/max compiled 0.00 sec, 3 clauses
6 ?-
| max(10,20,Z).
Z = 20.
13.PROLOG PROGRAM TO FIND OUT MAXIMUM OF TWO NUMBERS
SOURCE CODE
min(A,B,C):-A<B,C is A.
min(A,B,C):-B<A,C is B.
min(A,B,A).
OUTPUT
% c:/users/bum/documents/prolog/min compiled 0.00 sec, 4 clauses
7 ?-
| min(10,20,Z).
Z = 10
14. PROLOG PROGRAM TO SEE IF A LIST IS SORTED OR NOT
SOURCE CODE:
ord([_]).
ord([A,B|L]):-A<B,ord([B|L]).
OUTPUT
% c:/users/bum/documents/prolog/order compiled 0.00 sec, 3 clauses
8 ?- ord([3,6,7,10]).
true
15.Prolog Program to find Sum of the elements of the list
SOURCE CODE
sum([],0).
sum([X|T],Total):-sum(T,Temp),Total=Temp+X.
OUTPUT
% c:/users/bum/documents/prolog/sum compiled 0.00 sec, 3 clauses
9 ?- sum([2,3,4,5],R).
R = 0+5+4+3+2.
10
16.Prolog Program to find intersection of list.
SOURCE CODE
member( X, [X|Tail]).
member( X, [Head| Tail]) :- member( X, Tail).
inter([],_,[]).
inter([H1|T1],L2,[H1|RES]):-member(H1,L2),inter(T1,L2,RES).
inter([_|T1],L2,RES):-inter(T1,L2,RES).
OUTPUT
12 ?- inter([1,2,3,5],[8,1,6],Result).
Result = [1]
17.Prolog Program to find Union of two given list.
SOURCE CODE
union([H|T],[],[H|T]).
union([],[H|T],[H|T]).
union([H|T], SET2, RESULT) :- member(H,SET2), union(T,SET2,RESULT).
union([H|T], SET2, [H|RESULT]) :- not(member(H,SET2)), union(T,SET2,RESULT).
OUTPUT
26 ?- union([1,3], [3,5], Result),write(Result).
[1,3,5]
Result = [1, 3, 5] .
18.Write a Prolog program to find the permutation of the elements of a list.
SOURCE CODE
trace
domains
list = symbol*
predicates
del(symbol,list,list)
permute(list,list)
clauses
del(X,[X|L1],L1).
del(X,[Y|L1],[Y|L2]):-
del(X,L1,L2).
permute([],[]).
permute(L,[X|P]):-
del(X,L,L1),
permute(L1,P).
OUTPUT
% c:/users/bum/documents/prolog/permutation compiled 0.00 sec, 5 clauses
29 ?- permute([a,b,c],P).
P = [a, b, c] .
30 ?- permute([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] ;
19.Prolog Program for Selection Sort
SOURCE CODE
ssort([],[]).
ssort([M1|S],[H|T]):-min(H,T,M1),remove(M1,[H|T],N),ssort(S,N).
min(M,[],M).
min(M,[H|T],M1):-min2(M,H,N),min(N,T,M1).
min2(A,B,A):-less(A,B).
min2(A,B,B):-not(less(A,B)).
less(A,B):-(A<B).
append([],B,B).
append([H|A],B,[H|AB]):-append(A,B,AB).
remove(X,L,N):-append(A,[X|B],L),append(A,B,N).
OUTPUT
| ssort(S,[2,8,6,4]).
S = [2, 4, 6, 8]
20.Prolog Program for Merge sort
SOURCE CODE
mergesort([],[]).
mergesort([X],[X]).
mergesort([X,Y|Xs],Ys) :- split([X,Y|Xs],Xs1,Xs2), mergesort(Xs1,Ys1), mergesort(Xs2,Ys2), merge(Ys1,Ys2,Ys).
split([],[],[]).
split([X|Xs],[X|Ys],Zs) :- split(Xs,Zs,Ys).
merge([],Xs,Xs).
merge(Xs,[],Xs).
merge([X|Xs],[Y|Ys],[Z|Zs]) :- ( X =< Y -> Z = X, merge(Xs,[Y|Ys],Zs) ; Z = Y, merge([X|Xs],Ys,Zs)).
OUTPUT
5 ?- mergesort([8,1,5.6,4,2,3],Z).
Z = [1, 2, 3, 4, 5.6, 8]
21. Prolog Program for Insertion Sort
SOURCE CODE
sorting([A|B], Sorted) :- sorting(B, SortedTail), insert(A, SortedTail, Sorted).
sorting([], []).
insert(A, [B|C], [B|D]) :- A @> B, !, insert(A, C, D).
insert(A, C, [A|C]).
OUTPUT
8 ?- sorting([5, 4, 9, 1, 3, 8], X).
X = [1, 3, 4, 5, 8, 9].
22.Prolog Program for Quick Sort
SOURCE CODE
pivot(_, [], [], []).
pivot(Pivot, [Head|Tail], [Head|LessOrEqualThan], GreaterThan) :- Pivot >= Head, pivot(Pivot, Tail, LessOrEqualThan, GreaterThan).
pivot(Pivot, [Head|Tail], LessOrEqualThan, [Head|GreaterThan]) :- pivot(Pivot, Tail, LessOrEqualThan, GreaterThan).
quicksort([], []).
quicksort([Head|Tail], Sorted) :- pivot(Head, Tail, List1, List2), quicksort(List1, SortedList1), quicksort(List2, SortedList2), append(SortedList1, [Head|SortedList2], Sorted).
OUTPUT
% c:/users/bum/documents/prolog/quick compiled 0.00 sec, 6 clauses
24 ?-
| quicksort([8,1,5.6,4,2,3],Z).
Z = [1, 2, 3, 4, 5.6, 8]
23.Prolog Program for Bubble Sort
SOURCE CODE
bubble_sort(List,Sorted):-b_sort(List,[],Sorted).
b_sort([],Acc,Acc).
b_sort([H|T],Acc,Sorted):-bubble(H,T,NT,Max),b_sort(NT,[Max|Acc],Sorted).
bubble(X,[],[],X).
bubble(X,[Y|T],[Y|NT],Max):-X>Y,bubble(X,T,NT,Max).
bubble(X,[Y|T],[X|NT],Max):-X=<Y,bubble(Y,T,NT,Max).
OUTPUT
% c:/users/bum/documents/prolog/bubble compiled 0.00 sec, 7 clauses
2 ?- bubble_sort([2,3,1,4],L).
L = [1, 2, 3, 4]
24.PROLOG PROGRAM TO PRINT IF A NUMBER IS NATURAL NUMBER OR NOT
SOURCE CODE:
natural(N):-N>0,integer(N).
OUTPUT
% c:/users/bum/documents/prolog/int compiled 0.00 sec, 2 clauses
13 ?- natural(-5).
false.
14 ?- natural(5).
true.
25. WRITE A PROGRAM TO CHECK IF IT IS ODD OR EVEN
SOURCE CODE
check(N):-
N mod 2 ==0, write(‘ODD’).
check(N):-
N mod 2 =:=0, write(‘EVEN’).
OUTPUT
% c:/users/bum/documents/prolog/odd compiled 0.00 sec, 3 clauses
25 ?- check(6).
EVEN
true.
26. WRITE A PROGRAM TO FIND WHETHER A LIST IS A SUBLIST OF ANOTHER LIST
PROGRAM 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
% c:/users/bum/documents/prolog/checklist compiled 0.00 sec, 4 clauses
26 ?-
| sublist([c,d],[a,b,c,d,e,f]).
true
Topic : Write a Prolog program to add two numbers.
Algorithm:
Domains
element = integer
Predicates
add(element, element, element)
Clauses
add(X,Y,Z) :- Z is X+Y.
Goal
add(2,10,Z),write(Z).
1) Start
2) We perform the addition operation
3) Elements X, Y and Z are integer variables
4) Value 2 is assigned to X and values 10 is assigned to Y
5) Addition of X and Y is performed and stored in the Z variable
6) Value of Z is printed
7) End
Code:
add(X,0,X).
add(0,Y,Y).
add(X,Y,Z):-Z is X+Y.
?- add(2,10,Z),write(Z).
Output :
Topic : Write a Prolog program to find if number is odd or even.
Algorithm:
Domains
element = integer
Predicates
check(element).
Clauses
check(element) :- element mod 2==0,write(‘ODD’).
check(element) :- element mod 2=:=0,write(‘EVEN’).
Goals
check(123).
1) Start.
2) We find the given number is odd or even
3) A variable N is take which is an Integer variable
4) N is assigned the value of 123
5) N divided by 2 operation is performed. If it gives a remainder which is not equal to 0 then Odd is printed
6) If the remainder of N divided by 2 is 0 then even is printed
7) End
Code:
check(N):-N mod 2 ==0, write(‘ODD’).
check(N):-N mod 2 =:=0, write(‘EVEN’).
?- check(123).
Output :
Topic : Write a Prolog program to find the maximum of two numbers.
Algorithm:
Domains
element = integer
Predicates
max(element,element,element).
Clauses
max(A,B,C):-A>B,C is A.
max(A,B,C):-B>A,C is B.
Goals
max(107,20,Z),write(Z).
1) Start
2) A, B and C are three integer variables
3) A is assigned the value of 107 and B is assigned the value of 20
4) If A>B is true then C is assigned the value of A
5) If B>A is true then C is assigned the value of B
6) Value of C is printed
7) End
Code:
max(A,B,C):-A>B,C is A.
max(A,B,C):-B>A,C is B.
?-max(107,20,C),write(C).
Output :
Topic : Write a Prolog program to find the minimum of two numbers.
Algorithm:
Domains
element = integer
Predicates
min(element,element,element)
Clauses
min(A,B,C):-A<B,C is A.
min(A,B,C):-B<A,C is B.
Goals
min(10,20,Z),write(Z).
1) Start
2) A, B and C are three integer variables
3) A is assigned the value of 10 and B is assigned the value of 20
4) If A<B is true then C is assigned the value of A
5) If B<A is true then C is assigned the value of B
6) Value of C is printed
7) End
Code:
min(A,B,C):-A<B,C is A.
min(A,B,C):-B<A,C is B.
?- min(10,20,C),write(C).
Output :
Topic : Write a Prolog program to find the concatenation of two lists.
Algorithm:
Domains
list = integer*
Predicates
conc(list,list,list).
Clauses
conc([X|L1],L2,[X|L3]):-conc(L1,L2,L3).
Goals
conc([10],[1,2,3,4],R),write(R).
1) Start
2) X, Y and Z are three list type arguments
3) Value of two list are taken
4) First list(L1) is assigned the value of {10}
5) Second list(L2) is assigned the value of {1,2,3,4}
6) Third List(L3) takes the value concatenation of the first and the second list
7) Third list(L3) stores the value{10,1,2,3,4}
8) Value of L3 is printed
9) End
Code:
conc([],L,L).
conc([X|L1],L2,[X|L3]):-conc(L1,L2,L3).
?-conc([10],[1,2,3,4],R),write(R).
Output :
Topic : Write a Prolog program to find the reverse of a lists.
Algorithm:
Domains
list = integer*
Predicates
conc(list,list,list).
reverse(list,list).
Clauses
conc([X|L1],L2,[X|L3]):-conc(L1,L2,L3).
reverse([H|T], RevList):-reverse(T, RevT), conc(RevT, [H], RevList).
Goals
reverse([1,2,3,4,5],Result),write(Result).
1) Start
2) X, Y and Z are three list type arguments
3) Values of X and Y are taken and Z is the concatenated list of X and Y
4) A and B are two list type arguments where B store the value of reverse of the list A
5) Reverse operation is performed by the logic:
conc([X|L1],L2,[X|L3]):-conc(L1,L2,L3).
reverse([H|T], RevList):-reverse(T, RevT), conc(RevT, [H], RevList).
where L1, L2 are two lists. L3 is concatenated list of L1 and L2. RevT is the Reverse of the list T
6) List T is assigned the value {1,2,3,4,5}
7) Reverse operation is performed and RevT is printed
8) RevT stores the value {5,4,3,2,1}
9) RevT value is printed
10) End
Code:
conc([],L,L).
conc([X|L1],L2,[X|L3]):-conc(L1,L2,L3).
reverse(list, rev_List).
reverse([],[]).
reverse([H|T], RevList):-reverse(T, RevT), conc(RevT, [H], RevList).
?-reverse([1,2,3,4,5],Result),write(Result).
Output :
Topic : Write a Prolog program to append data to the end of list.
Algorithm:
Domains
element = integer
list = integer*
Predicates
insertAtEnd(element,list,list).
Clauses
insertAtEnd(X,[H|T],[H|Z]) :- insertAtEnd(X,T,Z).
Goals
insertAtEnd(10,[2,3,5],Result),write(Result).
1) Start
2) A is data
3) X and Y are list type arguments
4) A is the data that has to be inserted at the end of the list X
5) Y is the new list that is formed after inserting A in the list X
6) A is assigned the value {10}
7) X is assigned the value {2,3,5}
8) insertAtEnd(A,[H|X],[H|Y]) :- insertAtEnd(A,X,Y)operation is formed
9) Value of Y is printed
10) Y stores the value {2,3,5,10}
11) End
Code:
insertAtEnd(X,[ ],[X]).
insertAtEnd(X,[H|T],[H|Z]) :- insertAtEnd(X,T,Z).
?-insertAtEnd(10,[2,3,5],Result),write(Result).
Output:
Topic : Write a Prolog program to delete data from the beginning of the list.
Algorithm:
Domains
element = integer
list = integer*
Predicates
del(element,list,list).
Clauses
del(X,[Y|T],[Y|T1]):-del(X,T,T1).
Goals
del(1,[1,2,3,4,5],R),write(R).
1) Start
2) A is data
3) X and Y are list type arguments
4) A is the element that is to be deleted from the list X
5) Y is the new list formed after deleting the value A from the list X
6) A is assigned the value {1}
7) X is assigned the value {1,2,3,4,5}
8) del(X,[Y|T],[Y|T1]):-del(X,T,T1). Is performed.
9) Value of T1 is printed
10) T1 stores the value {2,3,4,5}
11) End
Code:
del(X,[X|T],T).
del(X,[Y|T],[Y|T1]):-del(X,T,T1).
?-del(1,[1,2,3,4,5],R),write(R).
Output:
Topic : Write a Prolog program to find the gcd of two numbers.
Algorithm:
Domains
element = integer
Predicates
gcd(element,element,element).
Clauses
gcd1(X,Y,Z) :- X>Y,D is X-Y,gcd1(D,Y,Z).
gcd1(X,Y,Z) :- X<Y,D is Y-X,gcd1(X,D,Z).
Goals
gcd1(12,18,Z),write(Z).
1) Start
2) X, Y,D and Z are integer variables
3) X is assigned the value 12and Y is assigned the value 18
4) GCD of X,Y id performed by – gcd(X,Y,Z) where Z store the value of gcd
5) If X>Y is true then D=X-Y and then gcd(X,D,Z) is performed
6) If X<Y is true then D=Y-X and then gcd(X,D,Z) is performed
7) Value of Z is printed
8) X stores the value 6
9) End
Code:
gcd1(X,X,X).
gcd1(X,Y,Z) :- X>Y,D is X-Y,gcd1(D,Y,Z).
gcd1(X,Y,Z) :- X<Y,D is Y-X,gcd1(X,D,Z).
?-gcd1(12,18,Z),write(Z).
Output:
Topic : Write a Prolog program to search a list for a value.
Algorithm:
Domains
X = integer
list = integer*
Predicates
search(element,list).
Clauses
search(X,[H|T]):-search(X,T).
Goals
search(3,[2,5,6,3]).
search(2,[2,5,6,3]).
1) Start
2) X is an integer value
3) Y is a list
4) X is the element to be searched in the list Y
5) X is assigned the value {3}
6) Y is assigned the value {2,5,6,3}
7) search(X,[H|T]):-search(X,T) is performed and value X is searched in the list T
8) If the value X is found the yes is printed else no is printed
9) Here yes is printed since 3 is there in the list {2,5,6,3}
10) End
Code:
search(X,[X|T]).
search(X,[H|T]):-search(X,T).
?-search(3,[2,5,6,3]).
?-search(2,[2,5,6,3]).
Output:
Topic : Write a Prolog program for family tree.
Algorithm:
1) Start
2) The predicates are set in the following way:
a. female(X). {where X is a string}
b. male(X). {where X is a string}
c. parent(X,Y). {where X is a string, Y is a string and X is the parent of X and Y}
3) The rules are stated in the following way :
a. child(X,Y) :- parent(Y,X).
b. father(X,Y) :- male(X), parent(X,Y).
c. grandfather(X,Y) :- male(X), parent(X,Z), parent(Z,Y).
d. mother(X,Y) :- female(X), parent(X,Y).
e. grandmother(X,Y) :- female(X), parent(X,Z), parent(Z,Y).
f. son(X,Y) :- male(X), parent(Y,X).
g. daughter(X,Y) :- female(X), parent(Y,X).
h. grandson(X,Y) :- male(X), parent(Z,X), parent(Y,Z).
i. granddaughter(X,Y) :- female(X), parent(Z,X), parent(Y,Z).
j. brother(X,Y) :- male(X), father(F,X), father(F,Y), mother(M,X), mother(M,Y).
k. sister(X,Y) :- female(X), father(F,X), father(F,Y), mother(M,X), mother(M,Y).
l. uncle(X,Y) :- male(X), brother(X,Z), parent(Z,Y).
m. aunt(X,Y) :- female(X), sister(X,Z), parent(Z,Y).
4) According to the rules set following are checked and the value of X is printed
a. grandmother(X,sudip), write(X).
b. grandson(X,mita),write(X).
c. grandfather(X,dipti), write(X).
d. aunt(X,amita), write(X).
5) End
Code:
female(ritu).
female(mita).
female(sonia).
female(dipti).
female(amita).
male(suman).
male(pradip).
male(rajib).
male(joy).
male(sudip).
parent(suman,pradip).
parent(suman,sonia).
parent(ritu,pradip).
parent(ritu,sonia).
parent(pradip,amita).
parent(mita,amita).
parent(amita,dipti).
parent(amita,joy).
parent(dipti,sudip).
parent(rajib,sudip).
child(X,Y) :-parent(Y,X).
father(X,Y) :-male(X), parent(X,Y).
grandfather(X,Y) :-male(X), parent(X,Z), parent(Z,Y).
mother(X,Y) :-female(X), parent(X,Y).
grandmother(X,Y) :-female(X), parent(X,Z),parent(Z,Y).
son(X,Y) :-male(X), parent(Y,X).
daughter(X,Y) :-female(X), parent(Y,X).
grandson(X,Y) :-male(X), parent(Z,X), parent(Y,Z).
granddaughter(X,Y) :-female(X), parent(Z,X),parent(Y,Z).
brother(X,Y) :-male(X), father(F,X), father(F,Y),mother(M,X), mother(M,Y).
sister(X,Y) :-female(X), father(F,X),father(F,Y), mother(M,X),mother(M,Y).
uncle(X,Y) :-male(X), brother(X,Z), parent(Z,Y).
aunt(X,Y) :-female(X), sister(X,Z),parent(Z,Y).
?- grandmother(X,sudip),write(X).
?- grandson(X,mita),write(X).
?- grandfather(X,dipti),write(X).
?- aunt(X,amita),write(X).
Output:
Topic : Write a Prolog program to find the last element of a list.
Algorithm:
Domains
list = integer*
Predicates
last(list).
Clauses
last([X]):-write(X).
last([Y|Tail]):-last(Tail).
Goals
last([1,2,3,4]).
1) Start
2) X is a list
3) X is assigned the value {1,2,3,4}
4) last([X]):-write(X)
last([Y|Tail]):-last(Tail) is performed where Tail is the last element in the string
5) Value of tail is printed
6) Here 4 is printed
7) End
Code:
last([X]):-write(X).
last([Y|Tail]):-last(Tail).
?-last([1,2,3,4]).
Output:
Topic : Write a Prolog program to find the length of a list.
Algorithm:
Domains
list = integer*
element = integer
Predicates
length(list,element).
Clauses
length([H|T],N):-length(T,N1),N is N1 + 1.
Goals
length([2,6,3,9,123],N),write(N).
1) Start
2) X is a list and Y is an integer which stores the length of the string
3) X is assigned the value {2,6,3,9,123}
4) length([H|T],N):-length(T,N1),N is N1 + 1 is performed to find the value of N where T is the string and N is the length of the string
5) Value of Y is printed. Here value of N is printed
6) Y stores the value 5. Here N is 5 as T is {2,6,3,9,123}
7) End
Code:
length([],0).
length([H|T],N):-length(T,N1),N is N1 + 1.
?-length([2,6,3,9,123],N),write(N).
Output:
Topic : Write a Prolog program to find the union of two lists.
Algorithm:
1) State the predicates :
a. member_of(X,Y). {X is an integer, Y is a list}
b. union(X,Y,Z). {Z is the union of lists X and Y}
2) State the rules :
a. member_of( X, [X|Tail]).
b. member_of( X, [Head| Tail]) :- member_of( X, Tail).
c. union([H|T],[],[H|T]).
d. union([],[H|T],[H|T]).
e. union([H|T],SET2,RES):-member_of(H,SET2),union(T,SET2,RES).
f. union([H|T],SET2,[H|RES]):-not(member_of(H|SET2)),union(T,SET2,RES).
3) State the goals :
a. union([1,2,3],[2,3],Result),write(Result).
Code:
member_of( X, [X|Tail]).
member_of( X, [Head| Tail]) :- member_of( X, Tail).
union([H|T],[],[H|T]).
union([],[H|T],[H|T]).
union([H|T],SET2,RES):-member_of(H,SET2),union(T,SET2,RES).
union([H|T],SET2,[H|RES]):-not(member_of(H|SET2)),union(T,SET2,RES).
?-union([1,2,3],[2,3],Result),write(Result).
Output:
Topic : Write a Prolog program to find the intersection of two lists.
Algorithm:
1) State the predicates :
a. member_of(X,Y). {X is an integer and Y is a list}
b. inter(X,Y,Z). {X,Y and Z are lists}
2) State the rules :
a. member_of( X, [X|Tail]).
b. member_of( X, [Head| Tail]) :- member_of( X, Tail).
c. inter([],_,[]).
d. inter([H1|T1],L2,[H1|RES]):-member_of(H1,L2),inter(T1,L2,RES).
e. inter([_|T1],L2,RES):-inter(T1,L2,RES).
3) State the goals :
a. inter([1,2,3,5],[8,6],Result),write(Result).
Code:
member_of( X, [X|Tail]).
member_of( X, [Head| Tail]) :- member_of( X, Tail).
inter([],_,[]).
inter([H1|T1],L2,[H1|RES]):-member_of(H1,L2),inter(T1,L2,RES).
inter([_|T1],L2,RES):-inter(T1,L2,RES).
?-inter([1,2,3,5],[8,6],Result),write(Result).
Output:
Topic : Write a Prolog program to find the sum of the elements of a list.
Algorithm:
Domains
element = integer
list = integer*
Predicates
sum(list,list).
Clauses
sum([X|T],Total):-sum(T,Temp),Total=Temp+X.
Goals
sum([12,3,4,5],R),write(R).
1) Start
2) X and Y are lists
3) Addition of the elements of the list is performed
4) sum([X|T],Total):-sum(T,Temp),Total=Temp+X where T is a list
5) X is assigned the value {12,3,4,5} and R is the sum total of all the elements of the list X
6) Value of R is printed. R stores the value 24
7) End
Code:
sum([],0).
sum([X|T],Total):-sum(T,Temp),Total=Temp+X.
?-sum([12,3,4,5],R),write(R).
Output:
Topic : Write a Prolog program to find the sublist of list.
Algorithm:
Domains
list = integer*
Predicates
sublist(list,list).
conc(list,list,list).
Clauses
sublist(S,L):-conc(L1,L2,L),conc(S,L3,L2).
conc([X|L1],L2,[X|L3]):-conc(L1,L2,L3).
Goals
sublist([c,d],[a,b,c,d,e,f]).
sublist([a,c,d],[a,b,c,d,e,f]).
1) Start
2) X and Y are list where Y is a sublist of X
3) conc(X,Y,Z) is performed where X,Y,Z are lists and Z is the concatenation of X and Y
4) sublist([c,d],[a,b,c,d,e,f])
sublist([a,c,d],[a,b,c,d,e,f])
these two operations are performed and checked whether the sublist is there or not.
5) If the sublist is there Yes is printed else no is printed
6) End
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).
?-sublist([c,d],[a,b,c,d,e,f]).
?-sublist([a,c,d],[a,b,c,d,e,f]).
Output:
Topic : Write a Prolog program to find the permutation of the elements of a list.
Algorithm:
Domains
element = character
list = string
Predicates
per(list,list).
del(element,list,list).
Clauses
per(L,[X|P]):-del(X,L,L1),per(L1,P).
del(X,[Y|T],[Y|T1]):-del(X,T,T1).
Goals
per([red,blue,green],P),write(P).
1) Start
2) X and Y are lists
3) per(X,Y) and del(X,Y,Z) is performed
4) per(L,[X|P]):-del(X,L,L1),per(L1,P) where del operation is done the following way: del(X,[Y|T],[Y|T1]):-del(X,T,T1)
5) In del(X,Y,Z) is the integer that is deleted from list Y and the result list is Z.
6) per([red,blue,green],p) is executed
7) Value of p is printed
8) End
Code:
per([],[]).
per(L,[X|P]):-del(X,L,L1),per(L1,P).
del(X,[X|T],T).
del(X,[Y|T],[Y|T1]):-del(X,T,T1).
?-per([red,blue,green],P),write(P).
Output:
Topic : Write a Prolog program to find the number of combination of the elements of a list.
Algorithm:
Domains
element = integer
Predicates
fact(element,element).
comb(element,element,element).
Clauses
fact(X,Y) :- X>0, T is X-1, fact(T,M), Y is X*M.
comb(N,R,C):- A is N-R, fact(N,G), fact(R,H), fact(A,I), J is H*I, C is G/J.
Goals
comb(8,4,R), write(R).
1) Start
2) X and Y are integer variables where Y is the factorial of X
3) To find the factorial of X we execute fact(X,Y) where x>0, tis X-1, fact(T,M) and Y is X*M
4) In comb(X,Y,Z) X,Y,X are integers where Z is the combination of X base Y
5) To find the number of combinations we execute comb(X,Y,Z) wher A is N-R, fact(N,G),fact(R,H), fact(A,I), j is H*I and C is G/J
6) comb(8,4,R)is performed where R stores the value of number of combinations.
7) Value of R is printed.
8) End
Code:
fact(0,1).
fact(X,Y):-
X>0, T is X-1, fact(T,M), Y is X*M.
comb(N,R,C):-
A is N-R,
fact(N,G),
fact(R,H),
fact(A,I),
J is H*I, C is G/J.
?- comb(8,4,R), write(R).
Output: