Escolar Documentos
Profissional Documentos
Cultura Documentos
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.
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.
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.