3

I need to create an expandable conditional that uses a loop (that is, a ..._map_function:). It should either return true from inside the loop, or return false otherwise. In a 'normal' programming language it would look something like this (where p is some predicate):

for each e in S {
    if p(e) {
        return true;
    }
}
return false;

A naive translation to LaTeX3 would look something like this (using l3prop as an example):

\cs_new:Nn \loop_body:nn {
    \bool_if:nn
        { p(#1, #2) }
        { \prg_return_true: }
}

\prop_map_function:NN \l_prop \loop_body:nn
\prg_return_false:

But that doesn't work, because \prg_return_false: will always appear in the input stream, even after a possible \prg_return_true:, and that is not allowed.

So how can this be done?

2

I'm not sure if this is the 'recommended' way, but it seems to work quite well:

\cs_new:Nn \loop_body:nn {
    \bool_if:nn
        { p(#1, #2) }
        { \prop_map_break:n { \use_i:nn \prg_return_true: } }
}

\prop_map_function:NN \l_prop \loop_body:nn
\prg_return_false:

\prop_map_break:n breaks the loop and puts some tokens in the input stream afterwards. \use_i:nn will then discard \prg_return_false:. This requires \prg_return_false: to appear immediately after the loop.

  • Given the functions currently available in expl3, yes, this is the solution I would suggest. – Bruno Le Floch May 24 '13 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.