Given a list L
of natural numbers I need to create a list containing the sums of the elements of all the subsets of L
. For example if L=[1,3,6]
I want to obtain the list [0,1,3,4,6,7,9,10]
.
I tried to use this code
subsetSums(List,Sums) :- findall(Sum,(subset(Sub,List),sum_list(Sub,Sum)),Sums).
but with the following query I get [0]
as the only result instead of [0,1,2,3]
?- subsetSums([1,2],Sums).
Where am I wrong?
EDIT: I'm working on SWI Prolog and subset/2
should be a native predicate.
subset/2
defined? Is it reallysubset(Subset, List)
or should it besubset(List, Subset)
? - lurkersubsetSums(List,Sums) :- findall(Sum,(list_to_set(List,Set),subset(Sub,Set),sum_list(Sub,Sum)),Sums).
- PrinceOfBorgosubset/2
only checks the property. Both params are input (+
). - Tomas Bysubset/2
did what you wanted,sum_list/2
does not work on sets. Just write your ownlist_subset/2
predicate. It's easy. Or at least you can find it half a dozen places here on SO. - lurker