Recursive LAMBDA() function to create a formula that adds internal separators to a string in Excel I have created a named function with signature PadInternal(base, width, paddingStr) where:

base is a string you want to add padding to
width is the length of the individual chunks
paddingStr is the string to pad with

called from a cell like:

PadInternal(&quot;Hello&quot;, 1, &quot; &quot;) = &quot;H e l l o&quot;
PadInternal(&quot;World&quot;, 3, &quot;**&quot;) = &quot;Wor**ld&quot;

And here's the function:




Tag
Description




Name
PadInternal


Scope
Workbook


Comment
base : a string you want to add padding to | width : the length of the individual chunks | paddingStr : the string to pad with


Refers To
=LAMBDA(base,width,paddingStr, IF(LEN(base)&lt;=width, base, LET(LHS, LEFT(base, width), RHS, RIGHT(base, LEN(base) - width), LHS &amp; paddingStr &amp; PadInternal(RHS,width,paddingStr))))




=LAMBDA(
    base,
    width,
    paddingStr,
    IF(
        LEN(
            base
        ) &lt;= width,
        base,
        LET(
            LHS,
            LEFT(
                base,
                width
            ),
            RHS,
            RIGHT(
                base,
                LEN(
                    base
                ) - width
            ),
            LHS &amp; paddingStr &amp;
            PadInternal(
                RHS,
                width,
                paddingStr
            )
        )
    )
)

Questions
As this is my first time using recursive Lambda functions in Excel, I'd like some feedback. In particular:

Is my algorithm efficient - I was thinking something with TEXTJOIN may be faster?
How could this be improved to take a dynamic array as &quot;base&quot;?
Can I have default values for the arguments?
What about meta data (formatting of the tooltip, scope etc), are there other ways to make my function more accessible?
I thought about this for a while and came up with some (what I think are) improvements:
=LAMBDA(base,width,paddingStr,TEXTJOIN(paddingStr,,MID(base,SEQUENCE((LEN(base)/width)+1,,1,width),width)))

This would:

No longer require a recursive LAMBDA() which should prove to be faster (untested)
Uses TEXTJOIN() which can handle errors and empty values internally so there is no longer any worry about that.
The use of MID() negates the use of LEFT(), RIGHT() etc.
No longer be using LET() which may also be using internal memory.