%int_puzzle(High, Number, List): returns a List of unique % integers between 1 and High that add up to Number % Only unique permutations of integers are returned. % This version finds the complete list of unique solutions. int_puzzle(High, Number, AllSolns) :- int_puzzle2(High, Number, [], AllSolns). % int_puzzle2(High, Number, OldSolns, AllSolns) solves the puzzle as % before, except OldSolns is the answer list so far, and AllSolns will % be the final list of all unique solutions. int_puzzle2(High, Number, OldSolns, AllSolns) :- make_intlist(1, High, All), make_soln(Number, All, Unsorted), sort(Unsorted, SortedSoln), % builtin to sicstus \+ member(SortedSoln, OldSolns), int_puzzle2(High, Number, [SortedSoln|OldSolns], AllSolns). int_puzzle2(_, _, Solns, Solns). % make_soln(Tot, All, Soln) tries to find integers from list All % that add up to Tot, returning soln in Soln make_soln(0, _, []). make_soln(Tot, All, [Num|List]) :- Tot > 0, remove(Num, All, Rest), NewTot is Tot - Num, make_soln(NewTot, Rest, List). % remove(A, L, R) removes item A from L, resulting in R. % This remove will fail if the item does not exist in the list. remove(N, [N|R], R). remove(N, [A|R], [A|S]) :- remove(N, R, S). % make a list of integers between N and M inclusive make_intlist(N, M, []) :- N > M. make_intlist(N, M, [N|L]) :- N =< M, N2 is N + 1, make_intlist(N2, M, L). % member(X, [X|_]). % member(X, [_|Y]) :- member(X, Y).