[Tex/LaTex] How to create lambda expressions

macros

It's common for packages to accept one- or two-argument macros as transformations of some element, and it's at least common for me for these to be one-offs that don't deserve a permanent name—or at least one in the user's namespace. How can I effect this?

\documentclass{article}
\usepackage{xcolor,empheq}

\begin{document}
\begin{empheq}[box=\colorbox{blue!20}{\hspace{1em}#1\hspace{1em}}]{align*}
  a & = b \\
  a^2 &= b^2
\end{empheq}
\end{document}

Consider the following attempt, heavily influenced by Define a New Macro via a Macro Using xparse Syntax:

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn

\cs_new:Npn \lambda_generate_new_csname: {
  \tl_set:Nn \l_tmpa_tl { random } % I'll figure this out later
  \prg_while:nn { ! \undefined_p \l_tmpa_tl } {
    \tl_set:Nn \l_tmpa_tl { another random }
  }
  \tl_use:N \l_tmpa_tl
}
\cs_new:Npn \lambda_generate_new_cs: {
  \use:c { \lambda_generate_new_csname: }
}

% number of arguments and then transformation
\NewDocumentCommand \LambdaFunction { m m } {
  \lambda_insert_lambda:nn { #1 } { #2 }
}

\cs_new_protected:Npn \lambda_insert_lambda:nn #1 #2 {
  \use:x {
    % expansion unsure
    \exp_not:n { \NewDocumentCommand } \lambda_generate_new_cs: {
      \prg_replicate:nn { #1 } { m }
    }
  } {
    #2
  }
}

\ExplSyntaxOff

\begin{document}

\LambdaFunction{1}{hello, #1!}
  {world}

% > hello, world!

\end{document}

EDIT

Acting on the comments, it would be very interesting to create an expandable LambdaFunction—one that would be more befitting of a functional programming style. I'd consider TeX as, after all, a functional language to a large extent—but does this extend to its very core? I think the answer to this question would say a lot about TeX's programming paradigm.

Best Answer

The diversion from a number to a list of m back (internally) to a list #1#2... seems unnecessarily long winded, I'd just do

\documentclass{article}

\newcommand\LambdaFunction[2][0]{%
\let\tmp\relax
\newcommand\tmp[#1]{#2}\tmp}

\begin{document}

\LambdaFunction[1]{hello, #1!}
  {world}

% > hello, world!

\LambdaFunction[2]{Good #1, Mr.~#2}
  {morning}{Sun}

\end{document}