5 views

Original Title: Prolog Exercise

Uploaded by Mj Ebrahim

- Chpt3 Notes acc112
- Chpt6 Notes acc112
- ITCS311Quiz
- Chpt8 Notes acc112
- Java Notes
- HTTP Handout
- ITCE314-CH2_Multiple_choice_questions-With+Solution
- Exercises - prolog
- MATHS211 TEST1 - 2007 Summer Semester
- H08The Analysis Phase
- itce 421
- Concept of programming languages
- Prolog Exercises
- ce321 exp1.docx
- Shoe Dog: A Memoir by the Creator of Nike
- Never Split the Difference: Negotiating As If Your Life Depended On It
- The Library Book
- Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
- Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
- Sapiens: A Brief History of Humankind

You are on page 1of 1

********************************************************************************* 1) sumLists(L, X): Define a predicate " sumLists " that takes a list L of lists of numbers, and returns a list of sums of lists in the given L.the minimum number X at any level in the list. If there is no number in the list, return 0. L = list of lists, X = list of sums.

?- sumLists([[2,3,4],[7,2.5,5.75],[9,17],[55],[]],X). X = [9,15.25,16,55,0]

sumList([],0). sumList([H|T],S):-sumList(T,TS), S is TS + H. sumLists([[]],[0]). sumLists([[X]], R):- sumList(X,R). sumLists([[H|T]],[A]):-sumList([H|T],A). sumLists([[H|T]|Tail],[SumX|SumTT]):- sumList([H|T],SumX), sumLists(Tail,SumTT).

2) sum7 (N, X): Define predicate(s) named sum7 that produces the sum X of all integers divisible by 7 between 0 and a given integer N. Zero is NOT considered.

?- sum7(30,X). X = 70 . ?- sum7(5,X). X = 0 . ?- sum7(60,X). X = 252

/* sum of all integers divisible by 7 between 0 and a given N */ sum7(N, Sum):- N>=0, N <7, Sum is 0. sum7(N, Sum):- N =:= 7, Sum is N. sum7(N, Sum):- N > 7, R is N mod 7, R =:=0, Q is N-7, sum7(Q, Qsum),Sum is Qsum+N. sum7(N, Sum):- N > 7, R is N mod 7, R =\=0, Q is N-1, sum7(Q, Qsum),Sum is Qsum+0.

Page# 1

- Chpt3 Notes acc112Uploaded byMj Ebrahim
- Chpt6 Notes acc112Uploaded byMj Ebrahim
- ITCS311QuizUploaded byMj Ebrahim
- Chpt8 Notes acc112Uploaded byMj Ebrahim
- Java NotesUploaded byMj Ebrahim
- HTTP HandoutUploaded byMj Ebrahim
- ITCE314-CH2_Multiple_choice_questions-With+SolutionUploaded byMj Ebrahim
- Exercises - prologUploaded byMj Ebrahim
- MATHS211 TEST1 - 2007 Summer SemesterUploaded byMj Ebrahim
- H08The Analysis PhaseUploaded byMj Ebrahim
- itce 421Uploaded byMj Ebrahim
- Concept of programming languagesUploaded byMj Ebrahim
- Prolog ExercisesUploaded byMj Ebrahim
- ce321 exp1.docxUploaded byMj Ebrahim