woryzower
woryzower

Reputation: 976

Prolog discarding permutation

I haven't been able to discard repetition permutations.For example, I want to get

?-permutation([1,2,1],L).
L = [1, 2, 1] ;
L = [1, 1, 2] ;
L = [2, 1, 1] ;

. but i am getting

?-permutation([1,2,1],L).
L = [1, 2, 1] ;
L = [1, 1, 2] ;
L = [2, 1, 1] ;
L = [2, 1, 1] ;
L = [1, 1, 2] ;
L = [1, 2, 1] ;

.

Upvotes: 0

Views: 87

Answers (1)

CapelliC
CapelliC

Reputation: 60034

I think the simpler way could be

perm_no_rep(L, P) :-
  setof(P, permutation(L, P), Ps),
  member(P, Ps).

it's almost ok:

?- perm_no_rep([1,2,1],P).
P = [1, 1, 2] ;
P = [1, 2, 1] ;
P = [2, 1, 1].

beware that will be factorial(Num_Distinct_Elements_in_L) = length(Ps) i.e. possibly very large lists.

Upvotes: 1

Related Questions