0% found this document useful (0 votes)
44 views

Assignment #3: Chapter 16 Prolog Exercises Date: NOV 2013

The document contains two Prolog programming exercises. The first asks to define a predicate "sumLists" that takes a list of lists of numbers and returns a list containing the sum of each nested list. The second asks to define a predicate "sum7" that calculates the sum of all integers between 0 and a given number N that are divisible by 7. Sample inputs and outputs are provided for each problem. Prolog code is then given as a solution to each problem.

Uploaded by

Mj Ebrahim
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Assignment #3: Chapter 16 Prolog Exercises Date: NOV 2013

The document contains two Prolog programming exercises. The first asks to define a predicate "sumLists" that takes a list of lists of numbers and returns a list containing the sum of each nested list. The second asks to define a predicate "sum7" that calculates the sum of all integers between 0 and a given number N that are divisible by 7. Sample inputs and outputs are provided for each problem. Prolog code is then given as a solution to each problem.

Uploaded by

Mj Ebrahim
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

University of Bahrain Department of Computer Science

College of Information Technology ITCS332: Concepts of Programming Languages

Assignment #3: Chapter 16 Prolog Exercises

Date: NOV 2013

********************************************************************************* 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.

ITCS 332 1st Semester 2013/2014 Two Exercizes for ass#3

Page# 1

You might also like