Anda di halaman 1dari 5

CSE 7 th Sem Paper Name :Atrificial Intelligence Labroratory Assignments Paper Code: CSE 702 ? 1.

Write a program in PROLOG to find out the GCD of two numbers. gcd(X,X,X). gcd(X,Y,D) :X<Y, Y1 is Y-X, gcd(X,Y1,D). gcd(X,Y,D) :Y<X, gcd(Y,X,D). ?- gcd(4,14,L),write(L). ? 2. Write a program in PROLOG to find out the maximum of two numbers. max(X,Y,X):X>=Y. max(X,Y,Y):X<Y. ?- max(84,74,J),write(J). ? 3. Write a program in PROLOG to find out the n-th Fibonacci number % 1 - terminating fibonacci(1, 0). % 2 - terminating fibonacci(2, 1). % 3 - doubly recursive fibonacci(Numb, Fib) :Numb > 2, Numb1 is Numb - 1, Numb2 is Numb - 2, fibonacci(Numb1, Fib1), fibonacci(Numb2, Fib2), Fib is Fib1 + Fib2. ?- fibonacci(6,D),write(D). ? 4. Write a program in PROLOG to find out factorial of a given number. factorial(0,1). factorial(N,F) :N>0, N1 is N-1, factorial(N1,F1), F is N * F1. ?- factorial(5,W),write(W). ? 5. Write a program in PROLOG to find out the length of a list. size1([],0). size1([H T],N) :- size1(T,N1), N is N1+1. % or size1([_ T],N) :- size1(T,N1), N is N1+1. ?- size1([1,2,3,4,[6,44]],N),write(N). %?- size([bill,ted,ming,pascal,nat,ron],N). %N = 6 %yes % ?- size([a, [b, c, d], e, [f g], h], N). %N = 5

%yes ? 6. Write a program in PROLOG to append two lists into a third one. append12([X Y],Z,[X W]) :- append12(Y,Z,W). append12([],X,X). ?- append([1,2,3],[4,5],A),write(A). ? 7. Write a program in PROLOG to reverse of a list. reverse([X Y],Z,W) :- reverse(Y,[X Z],W). reverse([],X,X). ?- reverse([1,2,3],[],A),write(A). ? 8. Write a program in PROLOG to check whether a list is palindrome or no t. palindrome(List) :- reverse(List,List). reverse(L1,L2) :- rev(L1,[],L2). rev([],L,L). rev([X L],L2,L3) :- rev(L,[X L2],L3). ?- palindrome([m,a,d,a,m]). ? 9. Write a program in PROLOG to find out the maximum of a list. max(X,Y,X) :X>=Y. max(X,Y,Y) :X<Y. maxlist([X],X). maxlist([X, Y Rest], Max) :maxlist([Y Rest],MaxRest), max(X,MaxRest,Max). ?- maxlist([45,65,53,5,34,32,1,4455,21,11132,24],Max),write(Max). ? 10. Write a program in PROLOG to determine the sum of the elements of l ist. sumlist([],0). sumlist([H T],N) :- sumlist(T,N1), N is N1+H. ?- sumlist([2,4,6],N),write(N). ? 11. Write a program in PROLOG to generate all the elements between two numbers. %range(I,K,L) :- I<=K,and L is the list containing all consecutive integers from 1 to K. range(I,I,[I]). range(I,K,[I L]):- I<K,I1 is I + 1, range(I1,K,L). ?- range(5,12,L),write(L). ? 12. Write a program in PROLOG to add an element to a list without duplic ation.(N.B: this Assignment to be done by the students themselves) member1(X,[X L]):-!. member1(X,[Y L]):-member1(X,L). add(X,L,L) :- member1(X,L),!. add(X,L,[X L]). ?- add(a,[a,b,c],L),write(L). 13. Write a program in PROLOG to delete operations on a given element. .(N.B: t his Assignment to be done by the students themselves) ? 14. Write a program in PROLOG to substitute an element X from a list L by an element A resulting in a new list M. %substitute all occurrences of O by N

subst(N,O,[],[]):- !. subst(N,O,[O T],[N R]):- subst(N,O,T,R),!. subst(N,O,[X T],[X R]):- subst(N,O,T,R). ?- subst(new,old,[old,a,old,b,c,old],R),write(R). 15. Write a program in PROLOG to trim(N,L,L1) such that L1 should be obtained f rom L without the 1st n-th element. .(N.B: this Assignment to be done by the stu dents themselves). 16. Write a program in PROLOG to trim(N,L,L1) such that L1 should be obtained from L without the last n-th element. .(N.B: this Assignment to be done by the students themselves). ? 17. Write a program in PROLOG to solve the Monkey-Banana Problem. % Monkey and banana problem % Description of legal moves move( state(middle, onbox, middle, hasnot), grasp, % Grasp banana state(middle, onbox, middle, has) ). move( state(P, onfloor, P, H), climb, % Climb box state(P, onbox, P, H) ). move( state(P1, onfloor, P1, H), push(P1, P2), % Push box from P1 to P2 state(P2, onfloor, P2, H) ). move( state(P1, onfloor, B, H), walk(P1, P2), % Walk from P1 to P2 state(P2, onfloor, B, H) ). % canget(State,Moves): monkey in State can get banana by performing Moves canget( state(_,_,_,has), [] ). canget(State1,[M List]) :- move(State1,M,State2), canget(State2,List). solve(Moves) :- canget(state(atdoor,onfloor,atwindow,hasnot),Moves). ?- solve(Moves),write(Moves). ? 18. Write a program in PROLOG to solve the Three Missionaries Three can nibals problem. % Main control block and printing find :path([3,3,left],[0,0,right],[[3,3,left]],_). output([]) :- nl, nl. output([[A,B,String] T]) :output(T), write(B), write(' ~~ '), write(A), write(': '), write(String), nl. % Base case path([A,B,C],[A,B,C],_,MoveList):nl,nl,output(MoveList). % Recursive call to solve the problem path([A,B,C],[D,E,F],Traversed,Moves) :move([A,B,C],[I,J,K],Out), legal([I,J,K]), % Don't use this move unless it's safe.

not(member([I,J,K],Traversed)), path([I,J,K],[D,E,F],[[I,J,K] Traversed],[ [[I,J,K],[A,B,C],Out] % Move commands and descriptions of the move

Moves ]).

move([A,B,left],[C,B,right],'One missionary crosses the river') :A > 0, C is A - 1. move([A,B,left],[C,B,right],'Two missionaries cross the river') :A > 1, C is A - 2. move([A,B,left],[C,D,right],'One missionary and One cannibal cross the river') : A > 0, B > 0, C is A - 1, D is B - 1. move([A,B,left],[A,D,right],'One cannibal crosses the river') :B > 0, D is B - 1. move([A,B,left],[A,D,right],'Two cannibals cross the river') :B > 1, D is B - 2. move([A,B,right],[C,B,left],'One missionary returns from the other side') :A < 3, C is A + 1. move([A,B,right],[C,B,left],'Two missionaries return from the other side') :A < 2, C is A + 2. move([A,B,right],[C,D,left],'One missionary and One cannibal return from the oth er side') :A < 3, B < 3, C is A + 1, D is B + 1. move([A,B,right],[A,D,left],'One cannibal returns from the other side') :B < 3, D is B + 1. move([A,B,right],[A,D,left],'Two cannibals return from the other side') :B < 2, D is B + 2. % Legal move definition where B is missionaries and A is cannibals: legal([B,A,_]) :(A =< B ; B = 0), C is 3-A, D is 3-B, (C =< D; D = 0). ?- find. ? 19. Write a program in PROLOG for Bubble sorting. 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). ?- bubble_sort([78,3,5,32,4534,3,55],L),write(L). ? 20. Write a program in PROLOG for Merge Sorting. mergesort([],[]). /* covers special case */ mergesort([A],[A]). mergesort([A,B R],S) :split([A,B R],L1,L2), mergesort(L1,S1), mergesort(L2,S2), merge(S1,S2,S). split([],[],[]). split([A],[A],[]). split([A,B R],[A Ra],[B Rb]) :- split(R,Ra,Rb). merge(A,[],A).

merge([],B,B). merge([A Ra],[B Rb],[A M]) :- A =< B, merge(Ra,[B Rb],M). merge([A Ra],[B Rb],[B M]) :- A > B, merge([A Ra],Rb,M). ?- mergesort([78,3,5,32,4534,3,55],L),write(L). 21. Write a program in PROLOG for Eight Queen problem ? 22. Write a program in PROLOG for Tower of Hanoi Problem. /* TOWERS OF HANOI */ hanoi(N) :- move(N,left,centre,rigth). move(0,_,_,_) :- !. move(N,A,B,C) :- M=N-1, move(M,A,C,B), inform(A,B), move(M,C,B,A). inform(X,Y) :- write([move,a,disc,from,the,X, pole,to,the,Y,pole]),n1. ?- hanoi(3). N.B: Assignments 13,15,16,21 to be done by the students themselves. Solution to the rest of the assignments are given in this e_mail.This e_mail to be forwarded to all the 4th Year,CSE students.

Some Extra Assignments ? Average of given numbers avg(N,A):-sum(N,K,Y),A is K/Y. sum([ ],0,0). sum([X L],S,Y):-sum(L,S1,Y1),S is S1+X,Y is Y1+1. ?- avg([1,2,14,4,3],Y),write(Y).

Anda mungkin juga menyukai