Partitions in PROLOG

Write a predicate part(S, I, L) which is true if and only if L is a list of all I-partitions of S, each appearing exactly once. L1 is an I-partition of S if (a) it contains I integers; (b) each integer in L1 is positive; (c) the integers in L1 are in non-increasing order; (d) the integers in L1 sum to S. The predicate can be written under the assumption that S and N will be given as numeric constants in the query.

Technically, a partition is a SET of integers, not a list. Requiring each partition to be "ordered" reduces the chance that your program will generate the same partition twice. In fact, a recursion can be devised that generates each distinct partition exactly once. This recursion is based on the idea that you can pick the first (largest) number in a partition, then generate all possible ways of choosing the remaining I-1 integers in the partition by a recursive call, and some data manipulation.

I found it helpful to use an "auxiliary" function, just like part(S, I, L), but with an additional parameter, K. partm(S, K, I, L) is true if and only if L is the list of all I-partitions of S such that no integer in L is larger than K.