943,697 Members | Top Members by Rank

Ad:
Mar 2nd, 2009
0

Prolog nested list sum

Expand Post »
Hi everybody, I'm having a little problem with finding sum of nested list in Prolog. So far what I have is
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.
Reputation Points: 29
Solved Threads: 0
Unverified User
BlackIris is offline Offline
8 posts
since Dec 2007
Mar 2nd, 2009
1

Re: Prolog nested list sum

Your second predicate is going to match and override whatever you do in your third predicate. You need to handle nested lists before you handle non-nested lists.
Team Colleague
Reputation Points: 1133
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Mar 3rd, 2009
0

Re: Prolog nested list sum

Thanks for the quick reply. While I agree with you but I have tried with third predicate before second and have encountered the same problem. I think the major problem lies with my logic in third predicate.
Reputation Points: 29
Solved Threads: 0
Unverified User
BlackIris is offline Offline
8 posts
since Dec 2007
Mar 3rd, 2009
1

Re: Prolog nested list sum

Well you have more problems than just that.
Team Colleague
Reputation Points: 1133
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Mar 4th, 2009
0

Re: Prolog nested list sum

So you mean I have logic problems and more...? Could you point me to a general direction, like where my logic is wrong (I know my logic is flawed but I'm not sure exactly what).
Reputation Points: 29
Solved Threads: 0
Unverified User
BlackIris is offline Offline
8 posts
since Dec 2007
Mar 4th, 2009
3

Re: Prolog nested list sum

All you need to do to fix your problem is look at your code and imagine what a prolog interpreter would do when interpreting it.
Team Colleague
Reputation Points: 1133
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Legacy and Other Languages Forum Timeline: pipes in gnuplot under windows
Next Thread in Legacy and Other Languages Forum Timeline: Which is better C# or JAVA?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC