Get all sets of list in prolog
Posted
by
garm0nboz1a
on Stack Overflow
See other posts from Stack Overflow
or by garm0nboz1a
Published on 2011-01-11T03:02:22Z
Indexed on
2011/01/13
22:53 UTC
Read the original article
Hit count: 118
prolog
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).
© Stack Overflow or respective owner