Professional Documents
Culture Documents
max(X,Y,M).
max(X,Y,M):-
X>Y,
M is X, write("\nMaximum
max(X,Y,M):-
X<Y,
M is Y, write("\nMaximum
X=Y,
equal").
3. Write a program in PROLOG to implement factorial (N, F) where F represents the factorial of a
number N.
F is 1, fact(N,F).
fact(N,F):- N>=1,
F1 is N*F, M
is N-1,
fact(M,F1).
fibonacci(N,T):-
N>=3,
Y is N-1, Z is
N-2,
fibonacci(Y,P),
fibonacci(Z,Q),
T is P + Q.
fibonacci(1,1). fibonacci(2,1).
5. Write a Prolog program to implement GCD of two numbers.
gcd(0,X):- write(X).
gcd(X,0):- write(X).
gcd(X,Y):- X
>= Y, G is X
mod Y,
gcd(G,X).
gcd(X,Y):- X <
Y, G is Y mod
X, gcd(X,G).
6. Write a prolog program to implement power(N,P,A) where N is raised to the power P to get A.
pow(N,0,1). pow(0,P,0).
pow(N,1,N).
pow(N,P,A):- P>0,
P1 is P-1,
pow(N,P1,A1),
A is N*A1.
7. Write a prolog program to implement muti(N,M,R) where N and M are the numbers to be
multiplied and R is the result. multiply:- write("\nEnter the first number : "), read(X), write("\nEnter
multi(X,Y,R):-
R is X*Y.
8. Write a program in PROLOG to implement towerofhanoi (N) where N represents the number
of Discs move(1,X,Y,_):- write("\n move disk from: "), write(X), write(" to "), write(Y).
move(N,X,Y,Z):-
N>1,
M is N-1,
move(M,X,Z,Y),
move(1,X,Y,_),
move(M,Z,Y,X).
9. Consider a cyclic directed graph [edge (p, q), edge (q, r), edge (q, r), edge (q, s), edge (s,t)] where
edge (A,B) is a predicate indicating directed edge in a graph from a node A to a node B. Write a
program to check whether there is a route from one node to another node. edge(p,q). edge(q,r).
member(X,[X|_]). member(X,[_|Y]):-
member(X,Y).
append([],L2,L2).
connected(X,Y):- edge(X,Y);edge(Y,X).
path(A,B,Path):- travel(A,B,[A],Q),
reverse(Q,Path).
travel(A,B,P,[B|P]):-connected(A,B).
travel(A,B,Visited,Path):-connected(A,C),
C \= B,
\+member(C,Visited),
travel(C,B,[C|Visited],Path).
10. Write a Prolog program to implement memb(X, L): to check whether X is a member of L or not.
member(X,[X|_]). member(X,[_|Y]):-
member(X,Y).
11. 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(List1,List2,Result),
write(Result).
conc([],List2,List2).
conc([H|List1],List2,[H|Result]):-
conc(List1,List2,Result).
12. Write a Prolog program to implement reverse (L, R) where List L is original and List R is
reversed list.
write(L2). reverse([],[]).
reverse([H|L1],L2):-
reverse(L1,L3),
append(L3,[H],L2).
13. Write a program in PROLOG to implement palindrome (L) which checks whether a list L is
a palindrome or not.
palindrome(L):-
reverse(L,L2),
compare(L,L2). reverse([],[]).
reverse([H|L],L2):-
reverse(L,L3),
append(L3,[H],L2).
compare([X|L],[X|L2]):-
compare(L,L2).
compare([X|L],[Y|L2]):- write("\nEntered
14. Write a Prolog program to implement sumlist(L, S) so that S is the sum of a given list L.
write(S).
sumlist([],0).
sumlist([X|Tail],Sum):-
sumlist(Tail,Temp), Sum
is X+Temp.
15. Write a Prolog program to implement two predicates evenlength(List) and oddlength(List) so
that they are true if their argument is a list of even or odd length respectively
start:- write("Enter the elements of the
even_odd(L).
length([],0). length([_|T],R):-
length(T,R1),
R is R1+1.
even_odd(L):- length(L,R),
16. Write a Prolog program to implement nth_element (N, L, X) where N is the desired position, L
is a list and X represents the Nth element of L.
start:- write("Enter elements in the list starting from 0th
element is : "),write(X).
n_element(0,X,[X|_]).
n_element(N,X,[_|Xs]):-
N>0,
N1 is N-1, n_element(N1,X,Xs).
17. Write a program in PROLOG to implement remove_dup (L, R) where L denotes the list with
some duplicates and the list R denotes the list with duplicates removed.
max_list(L,M).
max_list([H|L],M) :- M>=H,max_list(L,M). max_list([H|L],M) :- M<H,M1
is H,max_list(L,M1). max_list([],M) :- write('The maximum element in
the list is :- '),write(M).
19. Write a prolog program to implement insert_nth(I, N, L, R) that inserts an item I into Nth
position of list L to generate a list R.
insert_nth(I,1,L,[I|L]).
insert_nth(I,N,[H|L],[H|R]) :-
N>1, N1 is N-1,
insert_nth(I,N1,L,R).
20. Write a Program in PROLOG to implement sublist(S, L) that checks whether the list S is the
sublist of list L or not. (Check for sequence or the part in the same order).
21. Write a Prolog program to implement delete_nth(N, L, R) that removes the element on Nth
position from a list L to generate a list R.
22. Write a program in PROLOG to implement delete_all (X, L, R) where X denotes the element
whose all occurrences has to be deleted from list L to obtain list R.
start:-
write("Enter the list : "), read(L),
write("Enter the no to be deleted : "),
read(X), delall(X,L,R), write(R).
23. Write a program in PROLOG to implement merge (L1, L2, L3) where L1 is first ordered list and
L2 is second ordered list and L3 represents the merged list.
start :- write('Enter the first ordered list in [] :- ') , read(L1) ,
merge([] , [] , []).
merge([X|T1] , [] , [X|T]) :- merge(T1,[],T). merge([]
, [Y|T2] , [Y|T]) :- merge([],T2,T).
merge([X|T1],[Y|T2],[X|T]) :- X=<Y , merge(T1,[Y|T2],T).
merge([X|T1],[Y|T2],[Y|T]) :- X>=Y , merge([X|T1],T2,T).
24. Write a PROLOG program that will take grammar rules in the following format: NT ->NT | T)*
Where NT is any nonterminal, T is any terminal and Kleene star (*) signifies any number of
repetitions, and generate the corresponding top-down parser, that is:
sentence->noun-phrase, verb-phrase determiner
->[the]
will generate the following: sentence (I, O) :- noun-phrase(I,R), verb-
phrase (R,O).
determiner ([the|X], X) :- !.
s1-->np,vp. np--
>pn. np-->d,n,rel.
vp-->tv,np.
vp-->iv. rel-->[].
rel-->rpn,vp.
pn-->[PN],{pn(PN)}.
pn(mary). pn(henry).
rpn-->[RPN],{rpn(RPN)}.
rpn(that).
rpn(which).
rpn(who). iv--
>[IV],{iv(IV)}.
iv(runs). iv(sits). d-
->[DET],{d(DET)}.
d(a). d(the).
n--
>[N],{n(N)}.
n(book).
n(girl).
n(boy).
tv-->[TV],{tv(TV)}.
tv(gives). tv(reads).