# 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("Hello", 1, " ") = "H e l l o"


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)<=width, base, LET(LHS, LEFT(base, width), RHS, RIGHT(base, LEN(base) - width), LHS & paddingStr & PadInternal(RHS,width,paddingStr))))
=LAMBDA(
base,
width,
IF(
LEN(
base
) <= width,
base,
LET(
LHS,
LEFT(
base,
width
),
RHS,
RIGHT(
base,
LEN(
base
) - width
),
RHS,
width,
)
)
)
)


### 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 "base"?
• 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?
• PS, for a bit of context, I'm using this to format hexedecimal strings nicely with 2 character chunks Dec 16 '20 at 19:26
• Edit your post instead of commenting. Dec 16 '20 at 19:32
• @TomGebel normally I would, but for this I think it's enough to review the function in its own right and the use case is not actually that relevant to the question - I don't want to tie the function to a particular use case Dec 16 '20 at 19:41
• I haven't used the LAMBDA function quite yet, and this looks interesting. I'd change your function name though, something like ChunkMyText might do. :) Dec 17 '20 at 20:01

=LAMBDA(base,width,paddingStr,TEXTJOIN(paddingStr,,MID(base,SEQUENCE((LEN(base)/width)+1,,1,width),width)))

• 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.
• With the risk of appearing pedantic, a slight improvement would be to replace the +1 with +0.5 so that when the length is an even number the sequence does not generate an extra index which would be larger than the length thus resulting in an extra blank string to be joined. Nice solution! Jan 6 '21 at 15:21