Prolog Assignment
Prolog Assignment
sum(X,Y,Z):- Z is X+Y.
max(X,Y,M):-X>Y,M is X. max(X,Y,M):-Y>=X,M is Y.
fib(1,0).
fib(2,1).
fib(N1,X1),fib(N2,X2),X is X1+X2,!.
gcd(0,A,A):-!.
gcd(A,0,A):-!.
gcd(A,B,R):-B1 is mod(A,B),gcd(B,B1,R).
power(X,0):- !.
power(Num,Pow,Ans):-Ans is Num^Pow.
7. Prolog program to implement multi (N1, N2, R) : where N1 and N2 denotes the
numbers to be multiplied and R represents the result.
multi(X,0).
multi(N1,N2,R):- R is N1*N2.
move(M,X,Z,Y), move(1,X,Y,_),
move(M,Z,Y,X).
tower_of_hanoi(N) :- move(N,left,right,center).
memb(X,[X|Tail]).
memb(X,[Head|Tail]):-memb(X,Tail).
Write a Prolog program to implement conc (L1, L2, L3) where L2 is the list to be appended with
L1 to get the resulted list L3.
conc([],L,L).
conc([X|L1],L2,[X|L3]):-conc(L1,L2,L3).
10. Write a Prolog program to implement reverse (L, R) where List L is original and
List R is reversed list.
append([],L,L).
append([X|L1],L2,[X|L3]):- append(L1,L2,L3).
reverse([],[]).
reverse([H|T],R):- reverse(T,L1),append(L1,[H],R).
11. Write a program in PROLOG to implement palindrome (L) which checks whether a
list L is a palindrome or not.
app([],L,L).
app([X|L1],L2,[X|L3]):- app(L1,L2,L3).
pal([]). pal([_]).
pal(Plist):-app([H|T],[H],Plist),pal(T).
________