Prolog Write a predicate sumlistsL1 L2 L3 where L1 L2 L3 are

Prolog

Write a predicate sumlists(L1, L2, L3) where L1, L2, L3 are lists of integers. The predicate sumlists succeeds when L3 has a list of integers where each element is the sum of the corresponding elements from L1 and L2. For example sumlists([1,2,3], [3,4,5], L3) should return L3 = [4,6,8]. Handle the case where L1 and L2 are of different lengths (the output list L3 would have the length of the longer input list, and the elements in it after the shorter list ran out would just be equal to the elements in the longer list).

Solution

sumlists(T1,T2,T3). %keep chopping the heads off of the tails until there\'s no more to chop

output:

L3 = [5, 7, 9, 7].

sumlists([],[],[]). %Obviously the base case: L3 is the sum of two empty lists (an empty list)
Prolog Write a predicate sumlists(L1, L2, L3) where L1, L2, L3 are lists of integers. The predicate sumlists succeeds when L3 has a list of integers where each

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site