Try using an accumulator to keep track of the sum so far.
add(Lst,Sum) :- add(Lst,Sum,0).
add([],Sum,Sum).
add([X|Xs],Sum,Acc) :- NewAcc is Sum+Acc,
add(Xs,Sum,NewAcc).
I haven't tested it, but this should do the trick. The add/2 rule sets the accumulating helper to...
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.