# How to get the current number of iterations

Nicholasfox shared this question 2 years ago

I'm trying to complete a permutation using the iteration command.

In the expression below, "n" with underlined should be the number of iterations, but how can I get the current number of iterations?

Iteration(Join(Zip(Sequence(Insert(n, q, m), m, 1, n), q, p)), p, {{{1}}}, n)

Thanks. Afaik there is no way to access the current number of iterations directly.

In your case it's not really necessary though, because n is always the length of q, so you could just replace "n" by "length(q)". Although I guess that you rather want to replace it by "length(q)+1", assuming that you are trying to generate permutations. (And you could even start with "{{}}" instead of "{{1}}" so that n matches the length of the permutations.)

`Iteration(Join(Zip(Sequence(Insert(Length(q) + 1, q, m), m, 1, Length(q) + 1), q, p)), p, {{{}}}, n)`

A general workaround is to keep track of the current iteration by making it part of the list that is iterated over. It's always a bit annoying to write though and makes the Iteration command line even less readable. 2

Afaik there is no way to access the current number of iterations directly.

In your case it's not really necessary though, because n is always the length of q, so you could just replace "n" by "length(q)". Although I guess that you rather want to replace it by "length(q)+1", assuming that you are trying to generate permutations. (And you could even start with "{{}}" instead of "{{1}}" so that n matches the length of the permutations.)

`Iteration(Join(Zip(Sequence(Insert(Length(q) + 1, q, m), m, 1, Length(q) + 1), q, p)), p, {{{}}}, n)`

A general workaround is to keep track of the current iteration by making it part of the list that is iterated over. It's always a bit annoying to write though and makes the Iteration command line even less readable. 1

Got it. length(q)+1 is a great idea. Thank you very much.