0% found this document useful (0 votes)
54 views6 pages

Assignment 1

The document contains Prolog code snippets for various list processing and sorting algorithms including: summing and finding the maximum of lists, finding the length, greatest common divisor, reversing lists, sorting algorithms like merge sort, quicksort, insertion sort, and bubble sort. It also includes functions for list operations like finding subsets, intersections, unions, and dividing lists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views6 pages

Assignment 1

The document contains Prolog code snippets for various list processing and sorting algorithms including: summing and finding the maximum of lists, finding the length, greatest common divisor, reversing lists, sorting algorithms like merge sort, quicksort, insertion sort, and bubble sort. It also includes functions for list operations like finding subsets, intersections, unions, and dividing lists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

ASSIGNMENT 1

%1
ls([],0).
ls([H|T],Total):-
ls(T,Sum),Total is H+Sum.
% -----------------------------------------------------------------------
%2
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
my_length([],0).
my_length([_|L],N) :-
my_length(L,N1), N is N1 + 1.
% ------------------------------------------------------------------------
%4
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).
% ------------------------------------------------------------------------
%6
con([],L1,L1).

con([X|Tail],L2,[X|Tail1]):-
con(Tail,L2,Tail1).
% ------------------------------------------------------------------------
%7
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
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
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
kele(X,[X|_],1).
kele(X,[_|L],K) :-K > 1, K1 is K - 1, kele(X,L,K1).
%-----------------------------------------------------------
%3
findnum(X,[]):-
write("\nNumber Is Not Found").

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

findnum(X,[Y|Tail]):-
findnum(X,Tail).
%-----------------------------------------------------------
%4
member(X,List):-
delete(X,List,_).
delete(X,[X|Tail],Tail).

delete(X,[Y|Tail1],[Y|Tail2]):-
delete(X,Tail1,Tail2).
%-----------------------------------------------------------
%5
reverse_list(Inputlist,Outputlist):-
reverse(Inputlist,[],Outputlist).

reverse([],Outputlist,Outputlist).

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

%-----------------------------------------------------------
%6
add(X,List,[X|List]).
%-----------------------------------------------------------
%7
next_integer(I):-

next_integer(1,I).

next_integer(I,j).

next_integer(I,J):-

I2 is I+1.next_integer(I2,j)

%-----------------------------------------------------------
%8
my_last(X,[X]).
my_last(X,[_|L]) :- my_last(X,L).
ASSIGNMENT 3
%1

%-----------------------------------------------------------
%2
sublist([],[]).

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

sublist([_|Rest],Sub):-
sublist(Rest,Sub).
%-----------------------------------------------------------
%3
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
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
div([],[],[]).
div([X],[X],[]).
div([X,Y|List],[X|List1],[Y|List2]):-
div(List,List1,List2).

ASSIGNMENT 4

%1
max(X,Y,Y):-
X=<Y,!.
max(X,Y,X):-
X>Y.
%--------------------------------------------------------------------
%3
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
maximum([],Max):-

write(Max),n1

maximum([H|T],Max):- H>Max.

N=H, maximum(T,N).

maximum(L,Max):- maximum(L,Max).

%--------------------------------------------------------------------
%5
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
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
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
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
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
sorted([]).
sorted([A]).
sorted([A,B|T]) :- A=<B, sorted([B|T]).
sort(A,B) :- permutation(A,B), sorted(B).
%------------------------------------------------------------------------------------------------------

%5
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
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
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).

You might also like