I am trying to write a Prolog predicate (SWI) that will select N items from a list, for example:
selectn (+ N,? Elems,? List1,? List2) is true when List1 with all Elems removed results in List2.
selectn(N,Lps,L1s,[]) :- length(L1s,L), N >= L, permutation(L1s,Lps). selectn(0,[],L1s,Lps) :- permutation(L1s,Lps). selectn(N,[E|Es],L1s,L2s) :- select(E,L1s,L0s), N0 is N-1, selectn(N0,Es,L0s,L2s).
My problem is that in some cases I get duplicate results, and I don't know how to avoid them:
?- findall(L,selectn(2,Es,[a,b,c],L),Ls),length(Ls,Solutions). Ls = [[c], [b], [c], [a], [b], [a]], Solutions = 6.
This is not homework, but if you want to help me as if it were, I will be happy too.