SlideShare a Scribd company logo
1 of 1
Download to read offline
Write a Prolog function countAllX which counts the number of occurrences of an element in a
list, no matter how deeply nested they are. For example:
countAllX (A, [A B A [A 1 A [A A]]], N)
Returns N = 6
Thanks a lot for your help!!!
Solution
countAllX([],x,0).
countAllX([x|T],x,y):-countAllX(T,x,z),y is 1+z;
countAllX([x1|T],x,z):-x1=x,countAllX(T,x,z). // here we are creating a new dummy variable
called X1 that counts the number of occurences of a particular element
countall(LIST,x,c):-
sort(LIST,LIST1) , // this helps in sorting out the elements and helps in counting the number of
occurances of a particular number even if it is repeated
member(x,LIST1),
count(LIST,x,c).

More Related Content

More from suresh640714

Could please answer those questions about North country movieHere.pdf
Could please answer those questions about North country movieHere.pdfCould please answer those questions about North country movieHere.pdf
Could please answer those questions about North country movieHere.pdfsuresh640714
 
Consider the equation 1 - 2x = sin x. Use the Intermediate Value The.pdf
Consider the equation 1 - 2x = sin x.  Use the Intermediate Value The.pdfConsider the equation 1 - 2x = sin x.  Use the Intermediate Value The.pdf
Consider the equation 1 - 2x = sin x. Use the Intermediate Value The.pdfsuresh640714
 
CommunicationsEvery organization has its own unique “organization.pdf
CommunicationsEvery organization has its own unique “organization.pdfCommunicationsEvery organization has its own unique “organization.pdf
CommunicationsEvery organization has its own unique “organization.pdfsuresh640714
 
choose the word that BEST fits each statement.A. stomachB. Esoph.pdf
choose the word that BEST fits each statement.A. stomachB. Esoph.pdfchoose the word that BEST fits each statement.A. stomachB. Esoph.pdf
choose the word that BEST fits each statement.A. stomachB. Esoph.pdfsuresh640714
 
Based on your new understanding of Master Data Management and how or.pdf
Based on your new understanding of Master Data Management and how or.pdfBased on your new understanding of Master Data Management and how or.pdf
Based on your new understanding of Master Data Management and how or.pdfsuresh640714
 
B Ann baked more pans of lasagna. Each pan was shared with a differen.pdf
B Ann baked more pans of lasagna. Each pan was shared with a differen.pdfB Ann baked more pans of lasagna. Each pan was shared with a differen.pdf
B Ann baked more pans of lasagna. Each pan was shared with a differen.pdfsuresh640714
 
arMathAp11 6.3.021.Ask Your TeacherMy NotesHow much must be de.pdf
arMathAp11 6.3.021.Ask Your TeacherMy NotesHow much must be de.pdfarMathAp11 6.3.021.Ask Your TeacherMy NotesHow much must be de.pdf
arMathAp11 6.3.021.Ask Your TeacherMy NotesHow much must be de.pdfsuresh640714
 
Answer using basic programming beginner knowledge pls...........Othe.pdf
Answer using basic programming beginner knowledge pls...........Othe.pdfAnswer using basic programming beginner knowledge pls...........Othe.pdf
Answer using basic programming beginner knowledge pls...........Othe.pdfsuresh640714
 
Andy and Ben are going to play a final round of game 5 to determine .pdf
Andy and Ben are going to play a final round of game 5 to determine .pdfAndy and Ben are going to play a final round of game 5 to determine .pdf
Andy and Ben are going to play a final round of game 5 to determine .pdfsuresh640714
 
A die is rolled repeatedly until two consecutive rolls have the same .pdf
A die is rolled repeatedly until two consecutive rolls have the same .pdfA die is rolled repeatedly until two consecutive rolls have the same .pdf
A die is rolled repeatedly until two consecutive rolls have the same .pdfsuresh640714
 
A 70-kg individual drinks 2 Lday from a pond outside his house. Wha.pdf
A 70-kg individual drinks 2 Lday from a pond outside his house. Wha.pdfA 70-kg individual drinks 2 Lday from a pond outside his house. Wha.pdf
A 70-kg individual drinks 2 Lday from a pond outside his house. Wha.pdfsuresh640714
 
4.2.4. Suppose we assume that X1, X2, . . . , Xn is a random sample .pdf
4.2.4. Suppose we assume that X1, X2, . . . , Xn is a random sample .pdf4.2.4. Suppose we assume that X1, X2, . . . , Xn is a random sample .pdf
4.2.4. Suppose we assume that X1, X2, . . . , Xn is a random sample .pdfsuresh640714
 
2. If you have a nonlinear relationship between an independent varia.pdf
2. If you have a nonlinear relationship between an independent varia.pdf2. If you have a nonlinear relationship between an independent varia.pdf
2. If you have a nonlinear relationship between an independent varia.pdfsuresh640714
 
Write a MATLAB program that finds the maximum element in a matrix. Yo.pdf
Write a MATLAB program that finds the maximum element in a matrix. Yo.pdfWrite a MATLAB program that finds the maximum element in a matrix. Yo.pdf
Write a MATLAB program that finds the maximum element in a matrix. Yo.pdfsuresh640714
 
why is cell membrane semipermeableSolutioncell membrane needs .pdf
why is cell membrane semipermeableSolutioncell membrane needs .pdfwhy is cell membrane semipermeableSolutioncell membrane needs .pdf
why is cell membrane semipermeableSolutioncell membrane needs .pdfsuresh640714
 
Why are income statements and statement of cash flows dated similarl.pdf
Why are income statements and statement of cash flows dated similarl.pdfWhy are income statements and statement of cash flows dated similarl.pdf
Why are income statements and statement of cash flows dated similarl.pdfsuresh640714
 
Write a computer program as a Win32 console application in C to proc.pdf
Write a computer program as a Win32 console application in C to proc.pdfWrite a computer program as a Win32 console application in C to proc.pdf
Write a computer program as a Win32 console application in C to proc.pdfsuresh640714
 
Which of the following is not matched with its proper germ layer of .pdf
Which of the following is not matched with its proper germ layer of .pdfWhich of the following is not matched with its proper germ layer of .pdf
Which of the following is not matched with its proper germ layer of .pdfsuresh640714
 
What values were affected by simulate airway obstruction, and why Ex.pdf
What values were affected by simulate airway obstruction, and why Ex.pdfWhat values were affected by simulate airway obstruction, and why Ex.pdf
What values were affected by simulate airway obstruction, and why Ex.pdfsuresh640714
 
what is the shadow of irresponsibility and the shadow of privilege .pdf
what is the shadow of irresponsibility and the shadow of privilege .pdfwhat is the shadow of irresponsibility and the shadow of privilege .pdf
what is the shadow of irresponsibility and the shadow of privilege .pdfsuresh640714
 

More from suresh640714 (20)

Could please answer those questions about North country movieHere.pdf
Could please answer those questions about North country movieHere.pdfCould please answer those questions about North country movieHere.pdf
Could please answer those questions about North country movieHere.pdf
 
Consider the equation 1 - 2x = sin x. Use the Intermediate Value The.pdf
Consider the equation 1 - 2x = sin x.  Use the Intermediate Value The.pdfConsider the equation 1 - 2x = sin x.  Use the Intermediate Value The.pdf
Consider the equation 1 - 2x = sin x. Use the Intermediate Value The.pdf
 
CommunicationsEvery organization has its own unique “organization.pdf
CommunicationsEvery organization has its own unique “organization.pdfCommunicationsEvery organization has its own unique “organization.pdf
CommunicationsEvery organization has its own unique “organization.pdf
 
choose the word that BEST fits each statement.A. stomachB. Esoph.pdf
choose the word that BEST fits each statement.A. stomachB. Esoph.pdfchoose the word that BEST fits each statement.A. stomachB. Esoph.pdf
choose the word that BEST fits each statement.A. stomachB. Esoph.pdf
 
Based on your new understanding of Master Data Management and how or.pdf
Based on your new understanding of Master Data Management and how or.pdfBased on your new understanding of Master Data Management and how or.pdf
Based on your new understanding of Master Data Management and how or.pdf
 
B Ann baked more pans of lasagna. Each pan was shared with a differen.pdf
B Ann baked more pans of lasagna. Each pan was shared with a differen.pdfB Ann baked more pans of lasagna. Each pan was shared with a differen.pdf
B Ann baked more pans of lasagna. Each pan was shared with a differen.pdf
 
arMathAp11 6.3.021.Ask Your TeacherMy NotesHow much must be de.pdf
arMathAp11 6.3.021.Ask Your TeacherMy NotesHow much must be de.pdfarMathAp11 6.3.021.Ask Your TeacherMy NotesHow much must be de.pdf
arMathAp11 6.3.021.Ask Your TeacherMy NotesHow much must be de.pdf
 
Answer using basic programming beginner knowledge pls...........Othe.pdf
Answer using basic programming beginner knowledge pls...........Othe.pdfAnswer using basic programming beginner knowledge pls...........Othe.pdf
Answer using basic programming beginner knowledge pls...........Othe.pdf
 
Andy and Ben are going to play a final round of game 5 to determine .pdf
Andy and Ben are going to play a final round of game 5 to determine .pdfAndy and Ben are going to play a final round of game 5 to determine .pdf
Andy and Ben are going to play a final round of game 5 to determine .pdf
 
A die is rolled repeatedly until two consecutive rolls have the same .pdf
A die is rolled repeatedly until two consecutive rolls have the same .pdfA die is rolled repeatedly until two consecutive rolls have the same .pdf
A die is rolled repeatedly until two consecutive rolls have the same .pdf
 
A 70-kg individual drinks 2 Lday from a pond outside his house. Wha.pdf
A 70-kg individual drinks 2 Lday from a pond outside his house. Wha.pdfA 70-kg individual drinks 2 Lday from a pond outside his house. Wha.pdf
A 70-kg individual drinks 2 Lday from a pond outside his house. Wha.pdf
 
4.2.4. Suppose we assume that X1, X2, . . . , Xn is a random sample .pdf
4.2.4. Suppose we assume that X1, X2, . . . , Xn is a random sample .pdf4.2.4. Suppose we assume that X1, X2, . . . , Xn is a random sample .pdf
4.2.4. Suppose we assume that X1, X2, . . . , Xn is a random sample .pdf
 
2. If you have a nonlinear relationship between an independent varia.pdf
2. If you have a nonlinear relationship between an independent varia.pdf2. If you have a nonlinear relationship between an independent varia.pdf
2. If you have a nonlinear relationship between an independent varia.pdf
 
Write a MATLAB program that finds the maximum element in a matrix. Yo.pdf
Write a MATLAB program that finds the maximum element in a matrix. Yo.pdfWrite a MATLAB program that finds the maximum element in a matrix. Yo.pdf
Write a MATLAB program that finds the maximum element in a matrix. Yo.pdf
 
why is cell membrane semipermeableSolutioncell membrane needs .pdf
why is cell membrane semipermeableSolutioncell membrane needs .pdfwhy is cell membrane semipermeableSolutioncell membrane needs .pdf
why is cell membrane semipermeableSolutioncell membrane needs .pdf
 
Why are income statements and statement of cash flows dated similarl.pdf
Why are income statements and statement of cash flows dated similarl.pdfWhy are income statements and statement of cash flows dated similarl.pdf
Why are income statements and statement of cash flows dated similarl.pdf
 
Write a computer program as a Win32 console application in C to proc.pdf
Write a computer program as a Win32 console application in C to proc.pdfWrite a computer program as a Win32 console application in C to proc.pdf
Write a computer program as a Win32 console application in C to proc.pdf
 
Which of the following is not matched with its proper germ layer of .pdf
Which of the following is not matched with its proper germ layer of .pdfWhich of the following is not matched with its proper germ layer of .pdf
Which of the following is not matched with its proper germ layer of .pdf
 
What values were affected by simulate airway obstruction, and why Ex.pdf
What values were affected by simulate airway obstruction, and why Ex.pdfWhat values were affected by simulate airway obstruction, and why Ex.pdf
What values were affected by simulate airway obstruction, and why Ex.pdf
 
what is the shadow of irresponsibility and the shadow of privilege .pdf
what is the shadow of irresponsibility and the shadow of privilege .pdfwhat is the shadow of irresponsibility and the shadow of privilege .pdf
what is the shadow of irresponsibility and the shadow of privilege .pdf
 

Recently uploaded

Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxneillewis46
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................MirzaAbrarBaig5
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesAmanpreetKaur157993
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....Ritu480198
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSean M. Fox
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismDabee Kamal
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...Nguyen Thanh Tu Collection
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjMohammed Sikander
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFVivekanand Anglo Vedic Academy
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxAdelaideRefugio
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024Borja Sotomayor
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...Gary Wood
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint23600690
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project researchCaitlinCummins3
 

Recently uploaded (20)

Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 

Write a Prolog function countAllX which counts the number of occurre.pdf

  • 1. Write a Prolog function countAllX which counts the number of occurrences of an element in a list, no matter how deeply nested they are. For example: countAllX (A, [A B A [A 1 A [A A]]], N) Returns N = 6 Thanks a lot for your help!!! Solution countAllX([],x,0). countAllX([x|T],x,y):-countAllX(T,x,z),y is 1+z; countAllX([x1|T],x,z):-x1=x,countAllX(T,x,z). // here we are creating a new dummy variable called X1 that counts the number of occurences of a particular element countall(LIST,x,c):- sort(LIST,LIST1) , // this helps in sorting out the elements and helps in counting the number of occurances of a particular number even if it is repeated member(x,LIST1), count(LIST,x,c).