RosettaCodeData/Task/Permutations/Picat/permutations-2.picat

19 lines
394 B
Plaintext

import cp.
% Returns all permutations
permutation_cp1(N) = solve_all(X) =>
X = new_list(N),
X :: 1..N,
all_different(X).
% Find next permutation on backtracking
permutation_cp2(N,X) =>
X = new_list(N),
X :: 1..N,
all_different(X),
solve(X).
% Use the cp approach on a list L.
permutation_cp_list(L) = Perms =>
Perms = [ [L[I] : I in P] : P in permutation_cp1(L.len)].