It is common that I iterate over all permutations of 1,2...,n, either by making a table or performing a sum.
Instead of creating the set of all permutations, it would be better to iterate over them. Of course, it would be convenient for me if there was such a way to automatically do this iteration construct. The following is my attempt;
Clear[PermutationsTable];
SetAttributes[PermutationsTable, HoldFirst];
PermutationsTable[body_, {pi_Symbol, n_Integer}] := Module[
{iter},
Needs["Combinatorica`"];
Table[
Block[{pi = Combinatorica`UnrankPermutation[iter, n]},
body
]
, {iter, n!}]
];
Then one just does PermutationsTable[ First[pi], {pi, 4}] to say extract the first element out of all permutations of size 4.
Now, what improvements can be made for the code? Also, for the Sum version, I assume one can simply replace Table with Sum in the code.
There should be some way to color the iteration argument as how the variables work for
the build-in Table.