Sunteți pe pagina 1din 9

ASSIGNMENT 1

1. Write a prolog program to compute the sum of the list.


ls([],0).
ls([H|T],Total):-
ls(T,Sum),Total is H+Sum.
% -----------------------------------------------------------------------

2. Write a prolog program to find the maximum of two elements.


max(P,Q):-P>Q,write('Larger number is '),write(P).
max(P,Q):-P<Q,write('Larger number is '),write(Q).
max(P,Q):-P=Q,write('Both are equal ').
% -----------------------------------------------------------------------

3. Write a prolog program to find the length of the list.


my_length([],0).
my_length([_|L],N) :-
my_length(L,N1), N is N1 + 1.
% ------------------------------------------------------------------------

4. Write a prolog program to find the GCD.


gcd(X,Y):-X=Y,write('GCD of two numbers is '),write(X);
X=0,write('GCD of two numbers is '),write(Y);
Y=0,write('GCD of two numbers is '),write(X);
Y>X,Y1 is Y-X,gcd(X,Y1);
X>Y,Y1 is X-Y,gcd(Y1,Y).
% ------------------------------------------------------------------------

5. Write a prolog program to check same length.

sum(N1,N2) :-
checklength(N1,N2).

checklength(N1,N2) :-
L1 is length(N1,What),
L2 is length(N2,What),
Comp(L1,L2).

Comp(L1,L2) :-
L1=:=L2.

% ------------------------------------------------------------------------

6. Write a prolog program to concatenate of two list.


con([],L1,L1).

con([X|Tail],L2,[X|Tail1]):-
con(Tail,L2,Tail1).
% ------------------------------------------------------------------------

7. Write a prolog program to find out the maximum element of list.


maxlist([X],X).
maxlist([X,Y|T],MAX):-
X>Y,
maxlist([X|T],MAX).

maxlist([X,Y|T],MAX):-
X=<Y,
maxlist([Y|T],MAX).

% ------------------------------------------------------------------------

8. Write a prolog program to find out the factorial of an element.


fact(X,Y):-
X is 0, Y is 1;
X>0,N is X-1,fact(N,G),Y is X*G.

ASSIGNMENT 2

1. Write a prolog program to find fibonacci series.


fib(0,0).
fib(1,1).
fib(N,NF):-
N>1,
A is N-1,B is N-2,
fib(A,AF),fib(B,BF),
NF is AF+BF.
%-----------------------------------------------------------

2. Write a prolog program Find the K'th element of a list.


kele(X,[X|_],1).
kele(X,[_|L],K) :-K > 1, K1 is K - 1, kele(X,L,K1).
%-----------------------------------------------------------

3. Write a prolog program to test whether a list is not exactly of two


elements list.
findnum(X,[]):-
write("\nNumber Is Not Found").

findnum(X,[X|Tail]):-
write("\nNumber Is Found").

findnum(X,[Y|Tail]):-
findnum(X,Tail).
%-----------------------------------------------------------

4. Write a prolog program to determine whether an element X is a member


of a list L.
member(X,List):-
delete(X,List,_).
delete(X,[X|Tail],Tail).

delete(X,[Y|Tail1],[Y|Tail2]):-
delete(X,Tail1,Tail2).
%-----------------------------------------------------------

5. Write a prolog program to find the reverse of a list.


reverse_list(Inputlist,Outputlist):-
reverse(Inputlist,[],Outputlist).

reverse([],Outputlist,Outputlist).

reverse([Head|Tail],List1,List2):-
reverse(Tail,[Head|List1],List2).

%-----------------------------------------------------------
6. Write a prolog program to add an element.
add(X,List,[X|List]).
%-----------------------------------------------------------

7. Write a prolog program to define a predicate between which generates


all integers X.
next_integer(I):-
next_integer(1,I).
next_integer(I,j).
next_integer(I,J):-
I2 is I+1.next_integer(I2,j)

%-----------------------------------------------------------

8. Write a prolog program to find last element.


my_last(X,[X]).
my_last(X,[_|L]) :- my_last(X,L).

ASSIGNMENT 3

1. Write a prolog program to delete all occurrence of an element.


list=symbol*

deleteall(symbol,list,list)
%write this only if not running
deleteall(_,[],[]).
deleteall(X,[X|T],NT):-deleteall(X,T,NT).
deleteall(X,[H|T],[H|NT]):-H<>X,deleteall(X,T,NT).

%-----------------------------------------------------------

2. Write a prolog program to test whether a list X is a subset of a list Y.

sublist([],[]).

sublist([First|Rest],[First|Sub]):-
sublist(Rest,Sub).
sublist([_|Rest],Sub):-
sublist(Rest,Sub).
%-----------------------------------------------------------

3. Write a prolog program to intersect of two list X and Y.

intersection([X|Y],M,[X|Z]):-
list_member(X,M), intersection(Y,M,Z).
intersection([X|Y],M,Z):-
\+ list_member(X,M),intersection(Y,M,Z).
intersection([],M,[]).
list_member(X,[X|_]).
list_member(X,[_|TAIL]):-
list_member(X,TAIL).

%-----------------------------------------------------------

4. Write a prolog program to union of two list X and Y.


union([X|Y],Z,W):-
lm(X,Z), union(Y,Z,W).
union([X|Y],Z,[X|W]):-
\+ lm(X,Z) ,union(Y,Z,W).
union([],Z,Z).

lm(X,[X|_]).
lm(X,[_|Tail]):-
lm(X,Tail).
%-----------------------------------------------------------

5. Write a prolog program to divide a list in two list which are


appropriately of same length.

div([],[],[]).
div([X],[X],[]).
div([X,Y|List],[X|List1],[Y|List2]):-
div(List,List1,List2).

ASSIGNMENT 4

1. Write a prolog program to find the maximum of two elements using


CUT.
max(X,Y,Y):-
X=<Y,!.
max(X,Y,X):-
X>Y.
%--------------------------------------------------------------------

2. Write a prolog program to sum of a list using accumulator.

domains
x = integer
l = integer*

predicates
sum(l,x)

clauses
sum([],0).

sum([X|List],Sum) :-
sum(List,Sum1),
Sum = X + Sum1.

Output :

Goal: sum([1,2,3,4],Sum)
Sum=10
%--------------------------------------------------------------------

3. Write a prolog program to length of list using accumulator.


length(List, 0, N).
length([], N, N). % Second argument is the accumulator.
length([H|T], L, N) :-
L1 is L + 1,
length(T, L1, N).
%--------------------------------------------------------------------

4. Write a prolog program to find the maximum of a list elements using


CUT.
maximum([],Max):-
write(Max),n1
maximum([H|T],Max):- H>Max.
N=H, maximum(T,N).
maximum(L,Max):- maximum(L,Max).

output-
max([2,4,8,3],x)
x=8

%--------------------------------------------------------------------

5. Write a prolog program to GCD of two elements with CUT.


gcd(X, Y, G) :- X = Y, G = X.
gcd(X, Y, G) :-
X < Y,
Y1 is Y – X,
gcd(X, Y1, G).
gcd(X, Y, G) :- X > Y, gcd(Y, X, G).
%--------------------------------------------------------------------

6. Write a prolog program to find the GCD of list.


gcd(0,x,x):- x>0,1.
gcd(x,y,z):- x>=y, x1 is x-y, gcd(x1,y,z).
gcd(x,y,z):- x<y, x1 is y-x, gcd(x1,x,z).
gcdL([H,H1|T],Z):- gcd(H,H1,X), gcdL([H|T],Z).
gcdL([H1,H2],Z):- gcd(H1,H2,Z).

%--------------------------------------------------------------------

7. Write a prolog program to reverse of list using accumulator.

reverse(X, Xrev) :- reverse(X, [], Rev).

reverse([], Rev, Rev). % Nothing left to reverse.


reverse([H|T], Prev, Rev) :-
reverse(T, [H|Prev], Rev).

ASSIGNMENT 5

1. Write a prolog program to select an element from a list.


find([],N)
write(\"no such element\"),n1.
find([Elemrnt|List],1):-
write("element is",element),n1.
find([Elemrnt|List],N):-
N=n-1, find(list,N1).

%------------------------------------------------------------------------------------------------

2. Write a prolog program to sort all the elements of a list using merge sort.
ms([],[]).
ms([A],[A]).
ms([A,B|R],S):-
split([A,B|R],L1,L2),
ms(L1,S1),
ms(L2,S2),
merge(S1,S2,S).
split([],[],[]).
split([A],[A],[]).
split([A,B|R],[A|Ra],[B|Rb]):-
split(R,Ra,Rb).
merge(A,[],A).
merge([],B,B).
merge([A|Ra],[B|Rb],[A|M]):-
A=<B ,merge(Ra,[B|Rb],M).
merge([A|Ra],[B|Rb],[B|M]):-
A>B, merge([A|Ra],Rb,M).

%------------------------------------------------------------------------------------------------------------

3. Write a prolog program to sort all the elements of a list using quick sort.
quicksort([X|Xs],Ys) :-
partition(Xs,X,Left,Right),
quicksort(Left,Ls),
quicksort(Right,Rs),
append(Ls,[X|Rs],Ys).
quicksort([],[]).

partition([X|Xs],Y,[X|Ls],Rs) :-
X <= Y, partition(Xs,Y,Ls,Rs).
partition([X|Xs],Y,Ls,[X|Rs]) :-
X > Y, partition(Xs,Y,Ls,Rs).
partition([],Y,[],[]).

append([],Ys,Ys).
append([X|Xs],Ys,[X|Zs]) :- append(Xs,Ys,Zs).
%------------------------------------------------------------------------------------------------

4. Write a prolog program to sort all the elements of a list using


permutation sort.
sorted([]).
sorted([A]).
sorted([A,B|T]) :- A=<B, sorted([B|T]).
sort(A,B) :- permutation(A,B), sorted(B).
%------------------------------------------------------------------------------------------------------

5. Write a prolog program to sort all the elements of a list using insertion
sort.
insertSort([H|List], Result) :-
insertSort(List, Temp),
printlist(Temp),
insertItem(H, Temp, Result).
insertSort([], []).

insertItem(X, [H|List], [H|Result]) :-


H < X, !,
insertItem(X, List, Result).
insertItem(X, List, [X|List]).

printlist([]) :- nl.
printlist([X|List]) :-
write(X, " "),
printlist(List).
%------------------------------------------------------------------------------------------------

6. Write a prolog program to sort all the elements of a list using selection
sort.
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).

%----------------------------------------------------------------------------------------------------

7. Write a prolog program to sort all the elements of a list using bubble
sort.
bubblesort(InputList,SortList) :-
swap(InputList,List) , ! ,
printlist(List),
bubblesort(List,SortList).
bubblesort(SortList,SortList).

swap([X,Y|List],[Y,X|List]):- X > Y.
swap([Z|List],[Z|List1]) :- swap(List,List1).

printlist([] :-nl.
printlist([Head|List]) :-
write(Head, \" \"),
printlist(List).

S-ar putea să vă placă și