There are many great answers to this question, but here is another solution to this problem for you. This program differs from others in that it is very efficient and generates unreserved list solutions, which are supposed to be integers that add to the specified number.
gen(N, L) :- gen(N-1, N, N, FL), dup_n(FL, L). gen(CF, M, M, [CF]). gen(CF, S, M, [CF|R]) :- S < M, C > 1, C0 is C - 1, F0 is floor(M / C0), S0 is S + (C0 * F0), gen(C0-F0, S0, M, R). gen(CF, S, M, R) :- F > 0, F0 is F - 1, S0 is S - C, gen(C-F0, S0, M, R). dup_n([], []). dup_n([_-0|R], L) :- !, dup_n(R, L). dup_n([VF|R], [V|L]) :- F0 is F - 1, dup_n([V-F0|R], L).
Your implementation of addUpList/2 can be achieved by:
addUpList(N, P) :- findall(L, gen(N, L), P).
What should cause the following behavior:
?- addUpList(4,L). L = [[4], [3, 1], [2, 2], [2, 1, 1], [1, 1, 1, 1]].
Note that a list containing one 2 and two 1 appears only once in this result set; this is because gen/4 computes unique sets of integers that add up to the specified number.