code_junkie

Passing results in prolog

Nov 12th, 2011
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. sum([],0).
  2.  
  3. sum([N|L2],S) :- sum(L2,S2), plus(N,S2,S).
  4.  
  5. p([],1).
  6.  
  7. p([LL|L2],R) :- sum(LL,S), p(L2,P), mult(S,P,R).
  8.  
  9. prodsumlist([], 1).
  10.  
  11. prodsumlist([Head | Tail], Result) :-
  12. sumlist(Head, Sum_Of_Head),
  13. prodsumlist(Tail, ProdSum_Of_Tail),
  14. Result is Sum_Of_Head * ProdSum_Of_Tail.
  15.  
  16. ?- prodsumlist([[1, 2], [3], [-4]], Result).
  17. Result = -36.
  18.  
  19. prodsumlist(List, Result) :-
  20. xprodsumlist(List,1,Result).
  21.  
  22. xprodsumlist([],R,R).
  23.  
  24. xprodsumlist([Head|Rest],Sofar,Result) :-
  25. sumlist(Head, Sum_Of_Head),
  26. NewSofar is Sofar * Sum_Of_Head,
  27. xprodsumlist(Rest, NewSofar, Result).
Add Comment
Please, Sign In to add comment