递归问题

递归问题

我有一个递归如下:#1~{root} 和#2~{tree},以及 {tree}~{{node}{...{branch_j}...}} 和 branch_j={node}{tree}。对于最后一级,branch_j={node}{leaf}。我称“path”为 {root}...{node}。我想将 input={root}{tree} 映射到 {...{{path}{leaf}}...}(最终,我想这样做\__erw_process:nn{path}{leaf})。我的尝试只适用于代码中最多iter=3 的情况。有人能提出建议来解决这个问题吗?

\documentclass{article}
\usepackage{xparse}
\usepackage{unravel}
\ExplSyntaxOn
% ---
\cs_new_protected:Npn
\__erw_recurse:w #1 #2 / #3 #4 \q_recursion_stop 
{ \quark_if_recursion_tail_break:nN {#4} \tl_map_break:
  \tl_map_inline:nn{#2}
  {#1\__erw_recurse:w ##1 / #4 \q_recursion_stop} }

\cs_new_protected:Nn\erw_recurse_aux:nn
{ \__erw_recurse:w #1 / #2
  \q_recursion_tail
  \q_recursion_stop
  \prg_break_point:Nn \tl_map_break: {} }
\cs_generate_variant:Nn \erw_recurse_aux:nn{ne}

\cs_new_protected:Nn
\erw_recurse:nn
{ \texttt{iter=#2->}
  \erw_recurse_aux:ne{#1}
  { \prg_replicate:nn{#2}{ {iter} } } }
\cs_generate_variant:Nn \erw_recurse:nn{o}

% \use_none_delimit_by_q_recursion_stop:w⟨balanced text⟩\q_recursion_stop

\tl_set:Nn\l__erw_input_tl
{
  {curiosity} 
  {
    {
      {killed} 
      {
        {
          {the~cat}{leaf1}
        }
        {
          {the~mouse}{leaf2}
        }
      }
    }
    {
      {fulfilled}
      {
        {
          {the~cat}{leaf3}
        }
      }
    }
  }
}

% In this case, desired **output**,
% {{curiosity}{killed}{the~cat}}{leaf1}
% {{curiosity}{killed}{the~mouse}}{leaf2}
% {{curiosity}{fulfilled}{the~cat}}{leaf3}

\ExplSyntaxOff
\begin{document}
\ExplSyntaxOn

\erw_recurse:on{\l__erw_input_tl}{1}\par
\erw_recurse:on{\l__erw_input_tl}{2}\par
\erw_recurse:on{\l__erw_input_tl}{3}\par
\erw_recurse:on{\l__erw_input_tl}{4}\par

\ExplSyntaxOff
\end{document}

在此处输入图片描述

相关内容