You can't specify a macro that way; the #1
, #2
and so on must be literally present at definition's time, because they represent placeholders.
I don't see why limiting to six: you'll probably need the same for five or eight.
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\foo}{ O{,} m}
{
\mort_add_bar:nn { #1 } { #2 }
}
\seq_new:N \l_mort_input_seq
\seq_new:N \l_mort_output_seq
\cs_new_protected:Npn \mort_add_bar:nn #1 #2
{
% split the input at the comma (or what's in the optional argument)
\seq_set_split:Nnn \l_mort_input_seq { #1 } { #2 }
% clear the output section
\seq_clear:N \l_mort_output_seq
% put each item inside \bar{...}
\seq_map_inline:Nn \l_mort_input_seq
{
\seq_put_right:Nn \l_mort_output_seq { \bar{ ##1 } }
}
% output the sequence, separated by semicolons
\seq_use:Nn \l_mort_output_seq { ; }
}
\ExplSyntaxOff
\begin{document}
$\foo{a,b,c}$
% just to show that you can change the
% delimiter and have as many items as you wish
$\foo[.]{a.b.c.e.f.g.h.i}$
\end{document}

For drawing a number of objects, here's a possible solution:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\foo}{ O{,} m }
{
\mort_add_bar:nn { #1 } { #2 }
}
\seq_new:N \l_mort_input_seq
\cs_new_protected:Npn \mort_add_bar:nn #1 #2
{
\seq_set_split:Nnn \l_mort_input_seq { #1 } { #2 }
\seq_map_inline:Nn \l_mort_input_seq
{
\mort_draw_balls:n { ##1 }
}
}
\cs_new_protected:Npn \mort_draw_balls:n #1
{
/
\prg_replicate:nn { #1 } { \textbullet }
/
}
\ExplSyntaxOff
\begin{document}
\foo{1,2,5,0,1,2}
% just to show that you can change the
% delimiter and have as many items as you wish
\foo[-]{1-3-4}
\end{document}

\foreach
you use since there're more of them. Could you please convert your code into a Minimal (non-)Working Example?\argxiii
from\i
?\whiledo
is acceptable, or if you require\foreach
.