Combinations - back to content

	
		comb(N,L,Result). Combinations of N elements from the list L. 
		?- comb(2,[a,b,c],I).
		I = [a,b] 
		I = [a,c] 
		I = [b,c] 

		comb(N,L,X):- length(X,N),mem1(X,L).

rest(A,List,Rest). Returns the Rest of the list after the first occurrence of A. ?- rest(a,[a,b,c,d],R). R = [b,c,d] ?- rest(a,[b,c,a,d],I). R = [d] ?- rest(a,[b,c,d],R). R = [] rest(X,[],[]):- !. rest(X,[X|T],T):- !. rest(X,[_|T],R):- rest(X,T,R). mem1(Lr,L). Fills Lr with elements from L without repetitions ?- mem1([X,Y],[a,b,c]),write([X,Y]),fail. [a,b][a,c][b,a][b,c][c,a][c,b]no mem1([],Y). mem1([H|T],Y):- member(H,Y),rest(H,Y,New),mem1(T,New).