I have two functions, tuples
and perm
. They are similar.
tuples[L_, 0] := {{}};
tuples[L_, k_] := Join @@ Table[{x}~Join~y, {x, L}, {y, tuples[L, k - 1]}];
perm[L_, 0] := {{}};
perm[L_, k_] := Join @@ Table[{x}~Join~y, {x, L}, {y, perm[Select[L, # != x &], k - 1]}];
(*tuples[{1, 2, 3}, 2]*)
(*{{1, 1}, {1, 2}, {1, 3}, {2, 1}, {2, 2}, {2, 3}, {3, 1}, {3, 2}, {3, 3}}*)
(*perm[{1, 2, 3}, 2]*)
(*{{1, 2}, {1, 3}, {2, 1}, {2, 3}, {3, 1}, {3, 2}}*)
I know tuples
can be defined using Nest
, but I don't know whether perm
can be defined using Nest
or Fold
.
tuples2[L_, k_] := Nest[Join @@ Table[x~Join~{y}, {x, #}, {y, L}] &, {{}}, k];
perm
is supposed to do? Is it working correctly or not? And what specifically is your question? $\endgroup$perm
is equivalent toPermutations
. It looks like it is a question of how to implement it functionally. $\endgroup$Permutations
. For instance, the present implementation would not reproduce the behavior ofPermutations
with just one argument or with a list as a second argument. I think it's incumbent on the OP to tell us more precisely what he wants, rather than hoping our guesses will be correct. $\endgroup$perm
to produce the same output asPermutation
but in a different order? We don't know because the OP has not specified what the desired behavior is. $\endgroup$perm
is correct and does what the OP wants it to do (regardless of similarity toPermutation
), can it be written usingNest
orFold
?". $\endgroup$