0% found this document useful (0 votes)
306 views15 pages

Q-Divide A List in Two Lists Such That First List Contains Odd Position Elements and Second List Contain Even Position Elements

The document provides examples of Prolog code for various list processing tasks like dividing a list into odd and even elements, concatenating lists, implementing multiplication using addition, finding the factorial of a number, finding all elements except the last in a list, finding elements in the Fibonacci series, finding the greatest common divisor of two numbers, reversing a list, finding the maximum element in a list, checking if a list is a palindrome, and finding the length of a list. It also provides examples for sorting lists, binary trees, merging lists, removing duplicates from a list, and flattening nested lists.

Uploaded by

Prachi Saxena
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
306 views15 pages

Q-Divide A List in Two Lists Such That First List Contains Odd Position Elements and Second List Contain Even Position Elements

The document provides examples of Prolog code for various list processing tasks like dividing a list into odd and even elements, concatenating lists, implementing multiplication using addition, finding the factorial of a number, finding all elements except the last in a list, finding elements in the Fibonacci series, finding the greatest common divisor of two numbers, reversing a list, finding the maximum element in a list, checking if a list is a palindrome, and finding the length of a list. It also provides examples for sorting lists, binary trees, merging lists, removing duplicates from a list, and flattening nested lists.

Uploaded by

Prachi Saxena
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 15

Q-DIVIDE A LIST IN TWO LISTS SUCH THAT FIRST LIST CONTAINS ODD POSITION ELEMENTS AND SECOND LIST

CONTAIN EVEN POSITION ELEMENTS div([],[],[]). div([X],[X],[]). div([X,Y|L],[X|L1],[Y|L2]):-div(L,L1,L2). ?-div([a,b,c,d,e,f],L1,L2),WRITE("THE ODD POSITION ELEMENT LIST IS:-"),WRITE(L1),nl,WRITE("THE EVEN POSITION ELEMENT LIST IS:-"),WRITE(L2). OUTPUT THE ODD POSITION ELEMENT LIST IS:-[a,c,e] THE EVEN POSITION ELEMENT LIST IS:-[b,d,f]Yes.

Q-CONCATENATE THE TWO LIST. conc([],L,L). conc([X|L1],L2,[X|L3]):conc(L1,L2,L3). ?-conc([1,2,3,4],[5,6,7,8],L),WRITE(L). OUTPUT THE CONCATENATION LIST IS:-[1,2,3,4,5,6,7,8]

Q-IMPLEMENT MULTIPLICATION USING ADDITION multi(A,1,A). multi(A,0,0). multi(A,B,G):- B1 is B - 1, multi(A,B1,G1),G is A + G1. ?-multi(4,5,G),write("the multiplication of two no.is:"),write(G). OUTPUT the multiplication of two no. is:-20Yes.

Q-IMPLEMENT FACTORIAL OF A NUMBER. factorial(0,1). factorial(N,R):N1 is N - 1,factorial(N1 , R1),R is N * R1. ?-factorial(5,R),write("the factorial of a no. is:"),write(R).

OUTPUT the factorial of a no. is:-120Yes. Q-FIND ALL THE MEMBER EXCEPT THE LAST conc([],L,L). conc([X|L1],L2,[X|L3]):-conc(L1,L2,L3). last(List):conc(L1,[Item],List),WRITE("THE LIST EXCEPT THE LAST IS:-"),WRITE(L1). ?-last([1,2,3,4,5]). OUTPUT THE LIST EXCEPT THE LAST IS:-[1,2,3,4]Yes.

Q-FIND THE ELEMENT IN FIBBONACCI SERIES fibo(1,1). fibo(2,1). fibo(N,C):N1 is N - 1,N2 is N1 - 1,fibo(N1,C1),fibo(N2,C2),C = C1 + C2. ?-fibo(3,C),write("the third element of fibbonacci series is:-"),write(C). OUTPUT the third element of fibbonacci series is:-2Yes.

Q-FIND THE GCD OF TWO NUMBERS. gcd(M,N,G):N > M , gcd(N,M,G). gcd(M,0,M). gcd(M,N,G):N > 0,R is M mod N,gcd(N,R,G). ?-gcd(27,10,G) ,write("THE GCD OF TWO NO IS:-"),write(G). OUTPUT THE GCD OF TWO NO IS:-1Yes

Q-REVERSE THE LIST. conc([],L,L).

conc([X|L],M,[X|N]):-conc(L,M,N). reverse([],[]). reverse([X|L],R):reverse(L,R1),conc(R1,[X],R). ?-reverse([1,2,3,4,5],P),write("the reverse list is:-"),write(P). OUTPUT the reverse list is:-[5,4,3,2,1]Yes. Q-FIND THE MAXIMUM ELEMENT FROM THE 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([1,2,3,9,8],P),write("the maximum no. is:-"),write(P). OUTPUT the maximum no. is:-9Yes. Q-CHECK WHETHER THE LIST IS PALINDROM OR NOT. conc([],L,L). conc([X|L],M,[X|N]):-conc(L,M,N). reverse([],[]). reverse([X|L],R):-reverse(L,R1),conc(R1,[X],R). pali(L):-reverse(L,L). ?-pali([m,a,d,a,m]),write("list is palindrom "),nl. OUTPUT list is palindrom Yes. Q-FIND THE LENGTH OF LIST. length([],0). length([X|Y],L):-length(Y,L1),L is 1 + L1. ?- length([2,4,5,6,9],L) ,write("the length of the list is:-"), write(L). OUTPUT the length of the list is:-5Yes.

Q1-WAP using recursion (A) (i) quick sort gt(X,Y):-X>Y. conc([],L,L). conc([X|L1],L2,[X|L3]):-conc(L1,L2,L3). split(X,[],[],[]). split(X,[Y|Tail],[Y|Small],Big):gt(X,Y),!,split(X,Tail,Small,Big). split(X,[Y|Tail],Small,[Y|Big]):-split(X,Tail,Small,Big). qsort([],[]). qsort([X|Tail],Sorted):-split(X,Tail,Small,Big), qsort(Small,SortedSmall), qsort(Big,SortedBig), conc(SortedSmall,[X| SortedBig],Sorted). ?-qsort([3,6,9,14,20,4],Z),write("The Sorted List is ->"),write(Z). OUTPUT The Sorted List is ->[3,4,6,9,14,20]Yes. No. (ii)Insertion sort gt(X,Y):-X>Y,!. insort([],[]). insort([X|Tail],Sorted):insort(Tail,Sorted1),insert(X,Sorted1,Sorted). insert(X,[Y|Sorted],[Y|Sorted2]):gt(X,Y),!,insert(X,Sorted,Sorted2). insert(X,Sorted,[X|Sorted]). ?-insort([9,8,7,2],L),write(L). OUTPUT [2,7,8,9]Yes. (iii)Bubble sort swap([Z|Rest],[Z|Rest1]):- swap(Rest,Rest1). gt(X,Y):- X>Y. bubblesort(List,Sorted):swap(List,List1),!,bubblesort(List1,Sorted). bubblesort(Sorted,Sorted).

swap([X,Y|Rest],[Y,X|Rest]):- gt(X,Y). swap([Z|Rest],[Z|Rest1]):- swap(Rest,Rest1). ?-bubblesort([4,8,2],L),write("Sorted list is:"),nl,write(L). OUTPUT Sorted list is: [2,4,8] Yes. (C) fibonnaci fibo(0,P,Q):-!. fibo(X,Y,Z):-Sum is Y + Z, Y1 is Z, Z1 is Sum, write(Sum),nl, X1 is X - 1, fibo(X1,Y1,Z1). ?- fibo(5,1,1). OUTPUT 2 3 5 8 13 Yes. (D) factorial fact(0,1). fact(N,R):-N1 is N - 1,fact(N1,R1),R is N * R1. ?-fact(5,R),write(R). OUTPUT 120 Yes. (E) Generates a list of all permutations of the elements in a list of members. del(X,[X|T],T). del(X,[Y|T],[Y|T1]):-del(X,T,T1). per([],[]). per(L,[X|P]):-del(X,L,L1),per(L1,P).

?-per([red,green,blue],P),write(P). OUTPUT [red,green,blue]Yes. [red,blue,green]Yes. [green,red,blue]Yes. [green,blue,red]Yes. [blue,red,green]Yes. [blue,green,red]Yes. No. Q2-cyclic directed graph route(A,B):-edge(A,C),route(C,B). route(A,B):-edge(A,B). edge(p,q). edge(q,r). edge(q,s). edge(s,t). ?- route(p,t). ?- route(p,X),write(X),nl. OUTPUT Yes. t Yes. r Yes. s Yes. q Yes. No.

Q4-binary tree concat([],L,L). concat([X|L1],L2,[X|L3]):-concat(L1,L2,L3). b_tree(X,L,R). pre_btree(void,[]). pre_btree(b_tree(X,L,R),Y):pre_btree(L,L1),pre_btree(R,R1),concat([X|L1],R1,Y). in_btree(void,[]).

in_btree(b_tree(X,L,R),Y):in_btree(L,L1),in_btree(R,R1),concat(L1,[X|R1],Y). post_btree(void,[]). post_btree(b_tree(X,L,R),Y):post_btree(L,L1),post_btree(R,R1),concat(R1, [X],Y1),concat(L1,Y1,Y). ?pre_btree(b_tree(30,b_tree(25,b_tree(10,void,void),void), b_tree(60,void,b_tree(70,void,void))),Y), write("Preorder traversal is :- "),write(Y). ?in_btree(b_tree(30,b_tree(25,b_tree(10,void,void),void),b _tree(60,void,b_tree(70,void,void))),Y), write("Inorder traversal is :- "),write(Y). ?post_btree(b_tree(30,b_tree(25,b_tree(10,void,void),void) ,b_tree(60,void,b_tree(70,void,void))),Y), write("Postorder traversal is:- "),write(Y). OUTPUT Preorder traversal is :- [30,25,10,60,70]Yes. Inorder traversal is :- [10,25,30,60,70]Yes. Postorder traversal is:- [10,25,70,60,30]Yes. Q7-WAP for the following (A) merge(L1,L2,L3)-merge two ordered list L1,L2 and store in L3 merge(X,[],X). merge([],Y,Y). merge([X|X1],[Y|Y1],[X|Z]):-X<Y,!,merge(X1,[Y|Y1],Z). merge([X|X1],[Y|Y1],[X,Y|Z]):-X=Y,!,merge(X1,Y1,Z). merge([X|X1],[Y|Y1],[Y|Z]):-X>Y,!,merge([X|X1],Y1,Z). ?-merge([1,3,5],[2,4,6],X), write("after merging two ordered lists : "), write(X),nl. ?-merge([2,6,8],[1,3,4,5],X), write("after merging two ordered lists : "), write(X),nl. ?-merge([2,4,5],[2,4,5],X), write("after merging two ordered lists : "), write(X),nl. OUTPUT after merging two ordered lists : [1,2,3,4,5,6] Yes. after merging two ordered lists : [1,2,3,4,5,6,8]

Yes. after merging two ordered lists : [2,2,4,4,5,5] Yes. (C)To find last element of list conc([],L,L). conc([X|L1],L2,[X|L3]) :- conc(L1,L2,L3). last(List) :- conc(L1,[Item],List), write( " Last Element is : "),write(Item). ?-L=[1,2,3,4,5], last(L). OUTPUT Last Element is : 5 Yes. (D)To delete all occurences of a particular element from the list. del(_,[],[]). del(X,[X|T],M) :- del(X,T,M). del(X,[Y|T],[Y|T1]) :- del(X,T,T1). ?-L=[a,b,c,a,d,a,a], del(a,L,L1), write(L1). OUTPUT [b,c,d]Yes. (G)Remove duplicates member(X,[X|_]). member(X,[_|Y]):- member(X,Y). remdup(L,M):- dupacc(L,[],M). dupacc([],A,A). dupacc([H|T],A,L):- member(H,A),dupacc(T,A,L),!. dupacc([H|T],A,L):- dupacc(T,[H|A],L). ?- remdup([a,b,c,d,a,b,d,c,e,f,a],M),write(M). OUTPUT [f,e,d,c,b,a]Yes. No. Q8-flatten the given list. conc([],L,L). conc([X|L1],L2,[X|L3]):- conc(L1,L2,L3).

flatten([H|T],F):flatten(H,H1),flatten(T,T1),conc(H1,T1,F). flatten([],[]). flatten(X,[X]). ?-flatten([[a,b],[c],d,[[e]],f],X),write(X). OUTPUT [a,b,c,d,e,f]Yes. Q-Union member(X,[X|T]):-!. member(X,[H|T]):-member(X,T). union([],[],[]). union([],X,X). union(X,[],X). union([X|T],L,Z):-member(X,L),!,union(T,L,Z). union([X|T],L,[X|Z]):-union(T,L,Z). ?-L1=[a,c],L2=[b,d],write("\nL1 is :"),write(L1),write("\nL2 is :"),write(L2),union(L1,L2,L),write("\nUnion is:"),write(L). OUTPUT L1 is :[a,c] L2 is :[b,d] Union is:[a,c,b,d]Yes. No.

Q1- (B)To implement tower of Hanoi. hanoi(N):- move(N,left,center,right). move(0,_,_,_):-!. move(N,A,B,C):- M is N - 1,move(M,A,C,B), inform(A,B),move(M,C,B,A). inform(X,Y):- write(X), write(" to "), write(Y),nl. ?-hanoi(3). ?-write("the output of Tower Of Hanoi is : "),nl. OUTPUT left to center left to right center to right left to center right to left right to center

left to center Yes. the output of Tower Of Hanoi is : Yes. No. Q3-WAP to generate ntegers from 1-100 by backtracking on successive calls(use accumulator). int(N,Limit):- int(N,Limit,1). int(N,Limit,N):- write(N). int(N,Limit,I):- I < Limit, J is I + 1, int(N,Limit,J). ?-int(N,100). OUTPUT 1Yes. 2Yes. 3Yes. 4Yes. 5Yes. 6Yes. 7Yes. 8Yes. 9Yes. 10Yes. 11Yes. 12Yes. 13Yes. 14Yes. 15Yes. 16Yes. 17Yes. 18Yes. 19Yes. 20Yes. 21Yes. 22Yes. 23Yes. 24Yes. 25Yes. 26Yes. 27Yes. 28Yes. 29Yes. 30Yes. 31Yes. 32Yes. 33Yes. 34Yes. 35Yes.

36Yes. 37Yes. 38Yes. 39Yes. 40Yes. 41Yes. 42Yes. 43Yes. 44Yes. 45Yes. 46Yes. 47Yes. 48Yes. 49Yes. 50Yes. 51Yes. 52Yes. 53Yes. 54Yes. 55Yes. 56Yes. 57Yes. 58Yes. 59Yes. 60Yes. 61Yes. 62Yes. 63Yes. 64Yes. 65Yes. 66Yes. 67Yes. 68Yes. 69Yes. 70Yes. 71Yes. 72Yes. 73Yes. 74Yes. 75Yes. 76Yes. 77Yes. 78Yes. 79Yes. 80Yes. 81Yes. 82Yes. 83Yes. 84Yes. 85Yes. 86Yes.

87Yes. 88Yes. 89Yes. 90Yes. 91Yes. 92Yes. 93Yes. 94Yes. 95Yes. 96Yes. 97Yes. 98Yes. 99Yes. 100Yes. No. Q5-WAP for implementing login routine which prompts a user for the username and password. user(niti,niti1). user(richa,richa1). repeat. repeat:- repeat. login:- getdata(_,_),write("you are logged on"),nl. login:- repeat, write("Sorry,you are not permitted"),nl, write("try again"),nl,getdata(_,_),nl, write("you are logged on"),nl. getdata(N,P):- write("Enter login name"),nl, read(N), write("Enter password"),nl, read(P),user(N,P). ?- login. OUTPUT Enter login name Enter password you are logged on Yes. Q7(B)Reverse the elements of list. conc([],L,L). conc([X|L],M,[X|N]):-conc(L,M,N). reverse([],[]). reverse([X|L],R):-reverse(L,R1),conc(R1,[X],R).

?-reverse([1,2,3,4,5],P),write(P). OUTPUT [5,4,3,2,1]Yes. (F)-Delete all occurences of an element from the list. del(_,[],[]). del(X,[X|T],M):- del(X,T,M). del(X,[Y|T],[Y|T1]):- del(X,T,T1). ?- L = [a,b,c,a,d,a,a] , del(a,L,L1) , write(L1). OUTPUT [b,c,d]Yes. Q9-To find out the power of a number. power(X,1,X). power(X,M,N):- M1 is M - 1,power(X,M1,N1),N is N1 * X. ?-power(5,3,Y),write(Y). OUTPUT 125 Yes. Q6-Medical diagnostic system. go:- write("What is Patient Name?"),read(Patient),hypothesis(Patient,Disease),nl,writ e("Disease is: "),write(Disease),nl. go:- nl,write("Sorry,unable to diagnose"),nl. symptom(Patient,fever):- nl,write("Do you have a fever(y/n)?"),read(Reply),Reply='y'. symptom(Patient,rash):- nl,write("Do you have a rash(y/n)?"),read(Reply),Reply='y'. symptom(Patient,headache):- nl,write("Do you have a headache(y/n)?"),read(Reply),Reply='y'. symptom(Patient,running_nose):- nl,write("Do you have a runnig nose(y/n)?"),read(Reply),Reply='y'.

symptom(Patient,cough):- nl,write("Do you have a cough(y/n)?"),read(Reply),Reply='y'. symptom(Patient,conjuctivitis):- nl,write("Do you have a conjuctivitis(y/n)?"),read(Reply),Reply='y'. symptom(Patient,body_ache):- nl,write("Do you have a body ache(y/n)?"),read(Reply),Reply='y'. symptom(Patient,chills):- nl,write("Do you have chills(y/n)?"),read(Reply),Reply='y'. symptom(Patient,sore_throat):- nl,write("Do you have a sore throat(y/n)?"),read(Reply),Reply='y'. symptom(Patient,sneezing):- nl,write("Do you have sneezing(y/n)?"),read(Reply),Reply='y'. symptom(Patient,swollen_glands):- nl,write("Do you have a swollen_glands(y/n)?"),read(Reply),Reply='y'. hypothesis(Patient,measles):- symptom(Patient,fever), symptom(Patient,cough), symptom(Patient,conjuctivitis), symptom(Patient,running_nose), symptom(Patient,rash). hypothesis(Patient,german_measles):symptom(Patient,fever), symptom(Patient,headache), symptom(Patient,running_nose), symptom(Patient,rash). hypothesis(Patient,flu):- symptom(Patient,fever), symptom(Patient,headache), symptom(Patient,body_ache), symptom(Patient,conjuctivitis), symptom(Patient,chills), symptom(Patient,sore_throat), symptom(Patient,cough), symptom(Patient,running_nose). hyothesis(Patient,common_cold):symptom(Patient,headache), symptom(Patient,sneezing), symptom(Patient,sore_throat), symptom(Patient,cough), symptom(Patient,running_nose). hyothesis(Patient,mumps):- symptom(Patient,fever), symptom(Patient,swollen_glands).

hyothesis(Patient,chicken_pox):- symptom(Patient,rash), symptom(Patient,fever), symptom(Patient,body_ache), symptom(Patient,chills). hyothesis(Patient,whooping_cough):symptom(Patient,cough), symptom(Patient,sneezing), symptom(Patient,running_nose). ?-go. ?-symptom(Patient,fever). OUTPUT What is Patient Name?

Do you have a fever(y/n)? Do you have a fever(y/n)? Do you have a fever(y/n)? Do you have a headache(y/n)? Do you have a bodyache(y/n)? Do you have a headache(y/n)? Do you have a sneezing(y/n)? Do you have a fever(y/n)? Do you have a rash(y/n)? Do you have a cough(y/n)? Do you have a sneezing(y/n)? Do you have a running_nose(y/n)? Sorry,unable to diagnose Yes. Do you have a fever(y/n)? No.

You might also like