True when Xs is a permutation of Ys. This can
solve for Ys given
Xs or Xs given Ys, or even enumerate Xs
and Ys together. The predicate permutation/2
is primarily intended to generate permutations. Note that a list of
length N has N! permutations, and unbounded permutation generation
becomes prohibitively expensive, even for rather short lists (10! =
3,628,800).
If both Xs and Ys are provided and both lists
have equal length the order is |
Xs|
^
2.
Simply testing whether Xs is a permutation of Ys
can be achieved in order log(|
Xs|
)
using msort/2 as
illustrated below with the semidet
predicate is_permutation/2:
is_permutation(Xs, Ys) :-
msort(Xs, Sorted),
msort(Ys, Sorted).
The example below illustrates that Xs and Ys
being proper lists is not a sufficient condition to use the above
replacement.
?- permutation([1,2], [X,Y]).
X = 1, Y = 2 ;
X = 2, Y = 1 ;
false.
- Errors
type_error(list, Arg)
if either argument is not a proper or
partial list.