Wednesday, May 2, 2012

Prolog - writing a combination of k numbers in the given list

I am trying to define a function in prolog that takes arguments of the form combination(3,[a,b,c,d],L) , the result returns



L=a,b,c
L=a,b,d
L=a,c,d
L=b,c,d


My implementation is as follows:



combination(K,argList,L):-
unknown(X,argList,Y),
Z is select(X,argList),
length(Z,K),
L is Z,
combination(K,Z,L).

unknown(X,[X|L],L).
unknown(X,[_|L],R) :- unknown(X,L,R).


The unknown predicate behaves as follows:
![enter image description here][1]



Please help.





No comments:

Post a Comment