# expl3: making arguments from a loop

Here's what I'm trying to do:

\documentclass{minimal}
\usepackage{expl3}
\ExplSyntaxOn

\cs_new_protected:Npn \erw_loop:n #1
{
\int_zero:N \l_tmpa_int
\int_do_while:nNnn
{\l_tmpa_int} < {#1}
{
\int_incr:N \l_tmpa_int
{\int_to_Alph:n{\l_tmpa_int}}
}
{}
}

\begin{document}

\use_ii:nnn{A}{B}{C}\\ % B

\erw_loop:n{3}\\ % ABC

\use_ii:nnn\erw_loop:n{3} % 3 % would like B

\ExplSyntaxOff
\end{document}


I tried different versions of \exp_args: before \use_ii, no success. Perhaps you know a way that would work?

As with your other question this is an expansion problem. As indicated by its signature, \use_ii:nnn takes three arguments and return the second of those. In the example

\use_ii:nnn\erw_loop:n{3}


The first argument is \erw_loop:n, the second argument is 3, and the third is \ExplSyntaxOff which is a few lines down, but inside the ExplSyntax region spaces and newlines are ignored. This results in a pathological situation where you accidentally remove \ExplSyntaxOff, but it luckily doesn't do harm because the next statement is \end{document}. What you want is the following

\exp_last_unbraced:Nx \use_ii:nnn { \erw_loop:n { 3 } }


because this expands to

\use_ii:nnn ABC


given that \erw_loop:n { 3 } is fully expandable, which is also not given in your code. Not only is \erw_loop:n marked as protected which inhibits expansion from the start but it also performs assignments which are never expandable. Therefore I'd rewrite the body to

\int_step_function:nN { #1 } \int_to_Alph:n


The function \int_step_function:nN is conditionally expandable, meaning it is fully expandable if and only if all of its arguments are fully expandable. Since #1 has to be expandable anyway (it has to expand to a valid expression, digestible by \int_eval:n) and \int_to_Alph:n is expandable as well, this is ensured.

Still I think that this not a good way to deal with this. What if \erw_loop:n only returned two tokens? What if it returned many more than three tokens? But since the example looks extremely artificial and the use case is not clear to me, I cannot help you there.

\documentclass{article}
\usepackage{expl3}

\begin{document}

\ExplSyntaxOn

\cs_new:Npn \erw_loop:n #1
{
\int_step_function:nN { #1 } \int_to_Alph:n
}

\exp_last_unbraced:Nx \use_ii:nnn { \erw_loop:n { 3 } }

\ExplSyntaxOff

\end{document}