Using Lambda for Iterations or Recursion

How to use Lambda in Excel for iterative or recursive functions without VBA.

Watch the video below:

Using Lambda Function for Recursion

In my last video on Lambda functions I showed you how to create a Lambda function from scratch. It involved the following steps:

  1. Syntax of the function. For example: =LAMBDA([parameter1, parameter2, …,] calculation).
  2. A parameter is a value that we wish to pass to the function, such as a cell reference, string, or number. We can use up to 253 parameters.
  3. Calculation: This argument is required. It represents the formula we want to execute like multiplying two numbers and return the result of the function. It is the last argument and it must return a result.
  4. We call a lambda function. We call a lambda function the same manner we call native functions in Excel like PMT, SUM, Average, etc.
  5. We name a lambda function using the Name Manager

If we create a LAMBDA called ProductOfTwoNumbers for example, we can call ProductOfTwoNumbers within the definition of ProductOfTwoNumbers. This was earlier possible in Excel through VBA.

In the REMOVECHARS Lambda function, we have used the two text functions LEFT and RIGHT and the function SUBSTITUTE as shown below:

=LAMBDA(textString, illegalChars, IF(illegalChars=””, textstring, REMOVECHARS(
SUBSTITUTE(textString, LEFT(illegalChars, 1), “”), RIGHT(illegalChars, LEN(illegalChars)-1))))

We notice that in the definition of REMOVECHARS, there is a reference to REMOVECHARS. The IF statement tells us that if there are no more illegal characters then return the input String else remove each occurrence of the leftmost character in ‘illegalChars’. Recursion begins with the request to call REMOVECHARS again with the updated string, and the rest of ‘illegalChars’. This means it will keep calling itself until it has read every character to be removed.

Recursion Using lambda
Recursion Using lambda