Get all sets of list in prolog
- by garm0nboz1a
How can I generate all the possible sets of the elements of a list with current length?
get_set(X, [1,2,3]).
X = [1,1,1],
X = [1,1,2],
X = [1,1,3],
X = [1,2,1],
X = [1,2,2],
X = [1,2,3],
X = [1,3,1],
X = [1,3,2],
X = [1,3,3],
.....
X = [3,3,2],
X = [3,3,3].
UPD: there is good answer given by Sharky.
But maybe it's not the best. Here is another:
get_set(X,L) :- get_set(X,L,L).
get_set([],_).
get_set([X|Xs],[_|T],L) :- member(X,L), get_set(Xs,T,L).