Prolog nested list sum

Please support our Legacy and Other Languages advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2007
Posts: 8
Reputation: BlackIris is an unknown quantity at this point 
Solved Threads: 0
BlackIris BlackIris is offline Offline
Newbie Poster

Prolog nested list sum

 
0
  #1
Mar 2nd, 2009
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.
Han shot first
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,055
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Prolog nested list sum

 
1
  #2
Mar 2nd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 8
Reputation: BlackIris is an unknown quantity at this point 
Solved Threads: 0
BlackIris BlackIris is offline Offline
Newbie Poster

Re: Prolog nested list sum

 
0
  #3
Mar 3rd, 2009
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.
Han shot first
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,055
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Prolog nested list sum

 
1
  #4
Mar 3rd, 2009
Well you have more problems than just that.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 8
Reputation: BlackIris is an unknown quantity at this point 
Solved Threads: 0
BlackIris BlackIris is offline Offline
Newbie Poster

Re: Prolog nested list sum

 
0
  #5
Mar 4th, 2009
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).
Han shot first
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,055
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Prolog nested list sum

 
3
  #6
Mar 4th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Legacy and Other Languages Forum


Views: 1430 | Replies: 5
Thread Tools Search this Thread



Tag cloud for Legacy and Other Languages
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC