You are on page 1of 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