Reputation: 25
Iv'e been having problems with a prolog query that has some conditions hard to understand at first glance.
sameSum([L], V) :- !, sumlist(L,V).
sameSum([L|R], V) :- sumlist(L,V),sameSum(R,V).
sumlist([H],H) :- !.
sumlist([X|R],V) :- sumlist(R,Z), V is X+Z.
The query that is asked to be evaluated is:
?-sameSum([[6,7,2], [1,5,9], [8,3,4]], V).
?-sameSum([[6,4,1,2], [1,12], [8,5]], V).
?-sameSum([[6,7], [1,12], [8,5]], 13).
?-sameSum([],V).
Thank you in advance for the big help.
Upvotes: 2
Views: 76
Reputation: 10102
There is no need for any cuts here, instead:
sameSum([], _V).
sameSum([L|R], V) :-
% L = [_|_], % maybe
sumlist(L,V),
sameSum(R,V).
sumlist([],0).
sumlist([X|R],V) :-
sumlist(R,Z),
V is X+Z.
Note that your original definition failed for the last case you gave.
Upvotes: 3