Answers for "sum of a list prolog"

0

Write a prolog program to find the sum of elements in given list.

domains
    x = integer
    l = integer*
predicates
    sum(l,x)
clauses
    sum([],0).
    sum([X|List],Sum) :-
        sum(List,Sum1),
        Sum = X + Sum1.

Output :

Goal: sum([1,2,3,4],Sum)
Sum=10
1 Solution

Goal: sum([-2,-1,1,2],Sum)
Sum=0
1 Solution

Goal: sum([],Sum)
Sum=0
1 Solution

Goal: sum([1],Sum)
Sum=1
1 Solution
Posted by: Guest on April-26-2021
0

sum of a list prolog

sumlist(+List, -Sum)
Posted by: Guest on June-18-2021

Browse Popular Code Answers by Language