Beruflich Dokumente
Kultur Dokumente
%INSERTION SORT
gt(X,Y):-
X>Y.
insertsort([],[]).
insertsort([X|T],Sorted):-
insertsort(T,Sortedtail),
insert(X,Sortedtail,Sorted).
insert(X,[Y|Sorted],[Y|Sorted1]):-
gt(X,Y),!,insert(X,Sorted,Sorted1).
insert(X,Sorted,[X|Sorted]).
****************OUTPUT*****************
1 ?- insertsort([1,3,2,5,4,3],X).
X = [1, 2, 3, 3, 4, 5]
Q. % Quick sort
gt(X,Y):- X>Y.
conc([],L,L).
conc([H1|T1],L,[H1|T3]):-
conc(T1,L,T3).
quicksort([],[]).
quicksort([X|T],Sorted):-
split(X,T,Small,Big),
quicksort(Small,Sortedsmall),
quicksort(Big,Sortedbig),
conc(Sortedsmall,[X|Sortedbig],Sorted).
split(X,[],[],[]).
split(X,[Y|T],[Y|Small],Big):-
gt(X,Y),!,split(X,T,Small,Big).
split(X,[Y|T],Small,[Y|Big]):-
split(X,T,Small,Big).
****************OUTPUT*****************
?- quicksort([5,2,7,1,9],X).
X = [1, 2, 5, 7, 9]
Q. %Bubble Sort
gt(X,Y):-
X>Y.
bubblesort(List,Sorted):-
swap(List,List1),!,bubblesort(List1,Sorted).
bubblesort(Sorted,Sorted).
swap([X,Y|Rest],[Y,X|Rest]):-
gt(X,Y).
swap([Z|Rest],[Z|Rest1]):-
swap(Rest,Rest1).
****************OUTPUT*****************
1 ?- bubblesort([1,9,6,7,4],X).
X = [1, 4, 6, 7, 9]
Q. % To find permutation
del(X,[X|T],T).
del(X,[Y|T],[Y|T1]):-
del(X,T,T1).
insert(X,L,BL):-
del(X,BL,L).
per([],[]).
per([X|L],P):-
per(L,L1),
insert(X,L1,P).
****************OUTPUT*****************
1 ?- per([4,5,6],X).
X = [4, 5, 6] ;
X = [5, 4, 6] ;
X = [5, 6, 4] ;
X = [4, 6, 5] ;
X = [6, 4, 5] ;
X = [6, 5, 4] ;
No
Q. To implement towers of hanoi.
hanoi(N):-move(N,'A','B','C').
move(1,A,B,_):-write(' move disk from ' -A- ' to ' -B- '\n').
move(N,A,B,C):-M is N-1,
move(M,A,c,B),
move(1,A,B,_),
move(M,C,B,A).
****************OUTPUT*****************
1 ?- hanoi(3).
move disk from -A- to -c-
move disk from -A- to -c-
move disk from -B- to -c-
move disk from -A- to -B-
move disk from -C- to -c-
move disk from -C- to -B-
move disk from -A- to -B-
Yes
Q.%To find the nth element of a list
find1([X],0,[]).
find1([X|T],1,X).
find1([X|T],N,Z):-N1 is N-1,
find1(T,N1,Z).
****************OUTPUT*****************
1 ?- find1([1,2,3,4],4,X).
X=4
Q. %To append two lists
app([],L,L).
app([X|L1],L2,[X|L3]):-app(L1,L2,L3).
****************OUTPUT*****************
?- app([a,b],[c,d],L3).
L3 = [a, b, c, d]
Yes
Q.%To Delete all occurrences of an element.
add(X,L,[X|L]).
merge([],[],[]).
merge([],X,X).
merge(X,[],X).
merge([H|T],L2,L3):-merge(T,L2,L),add(H,L,L3).
del(X,L,L1):-delete(X,L,L1,[]).
delete(X,[],L1,L2):-reverse(L2,R),merge(R,T,L1).
delete(X,[X|T],L1,L2):-delete(X,T,L1,L2).
delete(X,[H|T],L1,L2):-add(H,L2,L),delete(X,T,L1,L).
****************OUTPUT*****************
?- del(a,[b,a,c,d,a,a,b],K).
K = [b, c, d, b]
Q.%FACTORIAL
fact(0,1).
fact(1,1).
fact(N,K):-N1 is N-1,fact(N1,K1),K is N*K1.
****************OUTPUT*****************
?- fact(4,K).
K = 24
Yes
Q.%FIBBONACCI
fib(1,1).
fib(2,1).
fib(N,K):-N1 is N-1,N2 is N-2,fib(N1,K1),fib(N2,K2),K is K1+K2.
****************OUTPUT*****************
?- fib(4,K).
K=3
Yes
Q.Multiplication of two numbers.
mul(X,0,0).
mul(X,Y,P):-Y > 0,Y1 is Y-1,mul(X,Y1,P1),P is P1+X.
****************OUTPUT*****************
?- mul(2,3,P).
P=6
Yes
Q.power of a no.
pow(X,0,1).
pow(X,P,S):-P>0,P1 is P-1,pow(X,P1,S1),S is X*S1.
****************OUTPUT*****************
?- pow(2,3,S).
S=8
Yes
Q.% PALINDROME OR NOT
add(X,[],[X]).
add(X,K,[X|K]).
rev(L,R):-rev1(L,[],R).
rev1([],L,L).
rev1([H|T],L,R):-add(H,L,L1),rev1(T,L1,R).
pal(L):-rev(L,R),L=R,print('yes');print('no').
****************OUTPUT*****************
?- pal([a,b,c,d,a,b]).
no
Q.REVERSE OF A LIST
add(X,[],[X]).
add(X,K,[X|K]).
rev(L,R):-rev1(L,[],R).
rev1([],L,L).
rev1([H|T],L,R):-add(H,L,L1),rev1(T,L1,R).
****************OUTPUT*****************
?- rev([1,2,3,4,5],K).
K = [5, 4, 3, 2, 1] ;
No