Você está na página 1de 13

1.WAP to find out the factorial of a number.

Domains
N=integer
R= integer
Predicates
fact(N,R).
Clauses
fact(0,1).
fact(N,R):- N>0,N1=N-1,fact(N1,R1),R=N*R1.
Goal:
fact(5,Res)
Res=120
1 Solution.
fact(6,Res)
Res=720
1 Solution.

2.WAP to find out the fibbonacci series.


Domains
N=integer
R= integer
Predicates
fib(N,R).
Clauses
fib(1,0).
fib(2,1).
fact(N,R):- N>2,N1=N-1,N2=N-2,fib(N1,R1),fib(N2,R2),R=R1+R2.
Goal:
fib(5,Res)
Res=3
1 Solution.
fib(6,Res)
Res=5
1 Solution.

3.WAP to find out the greatest common divisor(GCD).


Domains
A=integer
B= integer
C=integer
Predicates
gcd(A,A,A).
Clauses
gcd(A,0,A).
gcd(0,B,B).
gcd(A,A,A).
gcd(A,B,C):-B>A,gcd(B,A,C).
gcd(A,B,C):-A>B,N1=A-B,gcd(B,N1,C).
Goal:
gcd(15,10,Res)
Res=5
1 Solution.
gcd(4,5,Res)
Res=1
1 Solution.

4.WAP to count the number of element in a list.


Domains
N=integer
L= integer*
Predicates
count(L,N).
Clauses
count([],0).
count([H|T],N):-count(T,M),N=M+1.
Goal:
count([1,2,3,4],N)
N=4
1 Solution.
count([1,2,3,4,5,6],N)
N=6
1 Solution.

5.WAP to add and multiply the elements of the list.


Domains
S=integer
M=integer
L= integer*
Predicates
add(L,S).
mul(L,M).
Clauses
add([],0).
add([H|T],S):-add(T,J),S=J+H.
mul([],1).
mul([H|T],M):-mul(T,K),M=K*H.
Goal:
add([1,2,3],R)
R=6
1 Solution.
mul([1,2,3,4],R)
R=24
1 Solution.
add([1,2,3,4,5],R)
R=15
1 Solution.
mul([1,2,3,4,5],R)
R=120
1 Solution.

6.WAP to add and multiply the elements of the list using a single function.
Domains
S=integer
M=integer
L= integer*
Predicates
sumprod(L,S,M).
add(L,S).
mul(L,M).
Clauses
add([],0).
add([H|T],S):-add(T,J),S=J+H.
mul([],1).
mul([H|T],M):-mul(T,K),M=K*H.
sumprod(L,S,M):-add(L,S),mul(L,M).
Goal:
sumprod([1,2,3,4],X,Y)
X=10 , Y=24
1 Solution.
sumprod([1,2,3,4,5,6],X,Y)
X=21 , Y=720
1 Solution.

7.WAP to search an element from a list.


Domains
M=integer
L= integer*
Predicates
search(L,integer).
Clauses
search([H|T],H).
search([H|T],M):-search(T,M).
Goal:
search([1,2,3,4],4)
True
1 Solution.
sumprod([1,2,3,4,5,6],8)
False.
1 Solution.

8.WAP to insert an element at the first position of a list and at the nth position of a
list.
Domains
N=integer
P=integer
L= integer*
Predicates
insert(L,integer,L).
insertion(L,integer,integer,L).
Clauses
insert(L,N,[N|L]).
insertion(L,N,0,[N|L]).
insertion([H|T],N,P,[H|T1]):P1=P-1,insertion(T,N,P1,T1).
Goal:
insert([1,2,3,4],6,X)
X=[6,1,2,3,4]
1 Solution.
insert([1,2,3,4,5],8,X)
X=[8,1,2,3,4,5]
1 Solution.
insertion([1,2,3,4,5,6],8,Y)
Y=[1,2,3,4,5,6,8]
1 Solution.
insertion([1,2,3,4,5,6,7,8],9,Y)
Y=[1,2,3,4,5,6,8,9]
1 Solution.

9.WAP to delete an element from the first position of a list and from the nth position
of a list.
Domains
P=integer
L= integer*
Predicates
delete(L, L).
deletion(L,integer,L).
Clauses
delete([H:T],T).
deletion([H|T],0,T).
deletion([H|T],P,[H|T1]):P1=P-1,deletetion(T,P1,T1).
Goal:
delete([1,2,3,4],X)
X=[2,3,4]
1 Solution.
delete([1,2,3,4,5],X)
X=[2,3,4,5]
1 Solution.
deletion([1,2,3,4,5,6,7,8,9]Y)
Y=[1,2,3,4,5,6,7,8]
1 Solution.

10.WAP to concatenate two lists.


Domains
L=integer*
Predicates
con(L, L, L).
Clauses
con([], L1, L1).
con([X | T], L2, [X | T1]):- con(T, L2, T1).
Goal:
con([1,2,3],[4,5,6], X).
X=[1,2,3,4,5,6]
1 Solution.
con([1,2,3,4,5],[8,9,6], X).
X=[1,2,3,4,5,8,9,6]
1 Solution.

11.WAP to reverse a list.


Domains
L=integer*
Predicates
reverselist(L,L).
reverse(L, L, L).
Clauses
reverselist(L1,L2):-reverse(L1,[],L2).
reverse([], L2, L2).
reverse([H|T],L1,L2):-rev(T,[H|L1],L2).
Goal:
reverselist([1,2,3,4,5,6], X).
X=[6,5,4,3,2,1]
1 Solution.
reverselist([1,2,3,4,5,6,8,7], X).
X=[7,8,6,5,4,3,2,1]
1 Solution.

12.WAP to sort the elements of a list using insertion sort.

13.WAP to implement Bubble Sort.


Domains
L=integer*
Predicates
bubble_Sort(List,List).
b_Sort(List,List,List).
bubble(integer,List,List,integer).
Clauses
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).
Goal:
bubble_Sort([12,5,3,7],[],X).
X=[3,5,7,12]
1 Solution.
bubble_Sort([12,5,3,7,66,83,1],[],X).
X=[1,3,5,7,12,66,83]
1 Solution.

Você também pode gostar