I've been trying to use a macro which abstracts the structure of an \halign object to a number of columns, as mentioned in my previous question
"Can I use a macro in \halign's preamble?." For that part of the problem, \span worked well, but I haven't been able to get an integrated solution working.
I tried to use this code:
\count1=4 \countdef\columns=1
\def\subpre{
& ##
\advance\columns by -1 \ifnum\columns>0 \span\subpre \fi
}
\def\preamb{
##
\advance \columns by -1
\the\columns
\ifnum\columns>0 \span\subpre \fi
\cr
}
\halign{\span\preamb
first line \cr
second line \cr
third line \cr
}
\bye
to create an \halign which can be modified by changing the number which is assigned to count1, but this results in the error:
! TeX capacity exceeded, sorry [input stack size=5000].
\subpre ...by -1 \ifnum \columns >0 \span \subpre
\fi
\subpre ...by -1 \ifnum \columns >0 \span \subpre
\fi
\subpre ...by -1 \ifnum \columns >0 \span \subpre
\fi
\subpre ...by -1 \ifnum \columns >0 \span \subpre
\fi
\subpre ...by -1 \ifnum \columns >0 \span \subpre
\fi
\subpre ...by -1 \ifnum \columns >0 \span \subpre
\fi
...
l.13 \halign{\span\preamb
I think this is because the \span\subpre in \subpre automatically expands even if it's not supposed to be evaluated by the if statement, but I can't figure out how to expand it conditionally like in a "standard" recursive loop. What's the best way to conditionally expand the code within the if statement on line 4? I'm using Plain TeX.

\preambhas to be fully expandable. Can you use e-TeX features? – Henri Menke Jun 17 '19 at 01:49\halign&#\cr(or usually with some glue\halign&#\hfill\cr) – David Carlisle Jun 17 '19 at 06:55