How can I recursively process a comma-separated list in expl3? How to make a recursive Latex macro with comma separated argument list looks like a (more general) duplicate and offers no fewer than two expl3 answers. However, I'm obviously an idiot because I still can't figure out how to do it. I understand that expansion always confuses me, but I don't understand why none of the many variants I've played with have worked - or even given me errors. (OK, I do more-or-less understand why they don't give me errors, given that they don't work.)
I've also tried with sequences. Unsurprisingly, I couldn't get those to work either. I tried using an aux
function along (I thought) the lines shown in Scott H.'s answer. However, I apparently can't discern the salient features of that solution well enough to adapt it here. I didn't understand egreg's answer well enough to try anything.
\documentclass[border=10pt]{standalone}
\usepackage{expl3}
\begin{document}
\ExplSyntaxOn
\cs_new_protected_nopar:Nn \subsets_construct_subset:nn
{
\clist_set:Nn \l_tmpa_clist { #1 }
\int_zero:N \l_tmpa_int
\tl_clear:N \l_tmpa_tl
\clist_map_inline:Nn \l_tmpa_clist
{
\int_incr:N \l_tmpa_int
\int_compare:nF { \l_tmpa_int = #2 }{
\int_compare:nTF { #2 = 1 }{
\int_compare:nF { \l_tmpa_int = 2 } { \tl_put_right:Nn \l_tmpa_tl { , } }
}{
\int_compare:nF { \l_tmpa_int = 1 } { \tl_put_right:Nn \l_tmpa_tl { , } }
}
\tl_put_right:Nn \l_tmpa_tl { ##1 }
}
}
\tl_use:N \l_tmpa_tl
}
\cs_generate_variant:Nn \clist_set:Nn { Nx }
\subsets_construct_subset:nn { 2, 3, 4, 5 } { 2 } ~ : ~
\subsets_construct_subset:nn { 2, 4, 5 } { 1 } ~ : ~
\subsets_construct_subset:nn { \subsets_construct_subset:nn { 2, 3, 4, 5 } { 2 } } { 1 } ??
\ExplSyntaxOff
\end{document}
Sometimes, expansion seems more difficult to deal with in expl3 than it did before the revolution!
EDIT
I wanted to use this in an answer to The way to list subsets of the set {1, 2, 3, 4, 5} with forest or tree package. However, my answer there actually does it differently. I rewrote the Forest code in order to avoid the need for applying the expl3
function recursively. I wanted to avoid needing to do that, partly because a recursive approach seemed to reflect what was needed (or what I thought was needed) and partly because adding cycles to a Forest tree makes things very slow, very quickly. Also, I thought I must be missing some simple and obvious solution, as what I wanted seemed a pretty mundane sort of thing to need to do. So I wanted, as each node was added, to construct its content based on its parent's content and its n
value. However, to do this, its necessary to repeatedly apply the expl3
function as the tree grows. My current solution just adds the nodes and populates them only when the entire structure is in place (using an expl3
function very similar to the one Christian suggests below, as it happens). But this seems a less transparent approach ....
\subsets_construct_subset:nn { 2, 3, 4, 5 } { 2 }
as the second argument to\clist_set:Nn
will not build a clist and only one item will be seen; since\subsets_construct_subset:nn
doesn't work by expansion alone, I can't see what you're after.4,5
I think. The first strips the 3 from the2,3,4,5
list, leaving2,4,5
and this should be feeded again, stripping the first element in the list, so4,5
remains. But I agree that this not expandable and the\clist_set:Nn
will not generate a\clist
4,5
. I realise it doesn't work. But I don't know how to make it work. How can I get something which is expanded to the list produced by the first step, which I can feed back in for the second step?