| | |
Prolog nested list sum
Thread Solved |
•
•
Join Date: Dec 2007
Posts: 8
Reputation:
Solved Threads: 0
Hi everybody, I'm having a little problem with finding sum of nested list in Prolog. So far what I have is
With first two predicates, I have no problems getting sum of not-nested list. Anybody with a tip as to how I can modify the third predicate so that it can handle nested list? I've tried passing off [H] instead of just H and many other but it hasn't worked so far. Any comment would be deeply appreciated.
totalSum([],0).
totalSum([H|T],S):-
totalSum(T,Sum),
S is Sum + H.
totalSum([H|T],S):-
totalSum(H,Sum1),
totalSum(T,Sum2),
S is Sum1+Sum2.With first two predicates, I have no problems getting sum of not-nested list. Anybody with a tip as to how I can modify the third predicate so that it can handle nested list? I've tried passing off [H] instead of just H and many other but it hasn't worked so far. Any comment would be deeply appreciated.
Han shot first
![]() |
Other Threads in the Legacy and Other Languages Forum
- Previous Thread: pipes in gnuplot under windows
- Next Thread: Which is better C# or JAVA?
| Thread Tools | Search this Thread |






