Forum Discussion
Help Needed with Using Thunks with SCAN
- May 08, 2025
You were pretty much there
The changes I made were
- to remove the CHOOSE function to simplify the definition of arrays
- to avoid the (illegal) use of cell references as LET variables
- To simplify the evaluation of the result of the SCAN
The main issue was the fact that cell addresses are not valid as names within LET. I have also demonstrated the passing of functions as parameters and included a demonstration of the evaluation of arrays of thunks. Your formula using REDUCE offers the most effective approach if you only require values returned from the final step.
This is interesting but I have a couple of questions,
- valλ, LAMBDA([value1],[value2],[value3] are the Value1, value2 etc the starting values here?
- What does the ({1,2,3}) do in INDEX(arrϑ,3,1)({1,2,3})
[Value1], [Value2] and [Value3] are the 3 parameters of the valλ function. I made them optional in this case as a shortcut for the initial_value, which was defined as valλ(), but could have defined as valλ(0,0,0) for clarity. The 3 omitted arguments of the initial_value are essentially treated as zeros when recalled in the first iteration of SCAN; e.g.: prevλ(1)+1 = 0+1 = 1, prevλ(2)+2 = 0+2 = 2, prevλ(3)+3 = 0+3 = 3, and when these three results are passed to valλ(1,2,3), it returns an uncalled lambda function, =LAMBDA(index_num,CHOOSE(index_num,1,2,3)).
The result of arrϑ in this example is a vertical vector of uncalled lambda functions as follows:
=LAMBDA(index_num,CHOOSE(index_num,1,2,3))
=LAMBDA(index_num,CHOOSE(index_num,2,4,6))
=LAMBDA(index_num,CHOOSE(index_num,3,6,9))INDEX(arrϑ,3,1) returns the last item from the vector of functions, so INDEX(arrϑ,3,1)({1,2,3}) is the same as saying =LAMBDA(index_num,CHOOSE(index_num,3,6,9))({1,2,3}), which is calling all three values from CHOOSE and returning {3,6,9}.
For clarity, the first example shown in my previous post could be rewritten as follows:
=LET(
valλ, LAMBDA([value1],[value2],[value3],LAMBDA(index_num,CHOOSE(index_num,value1,value2,value3))),
init, valλ(0,0,0),
arrϑ, SCAN(init,SEQUENCE(3),LAMBDA(prevλ,i,LET(
x, prevλ(1)+1,
y, prevλ(2)+2,
z, prevλ(3)+3,
valλ(x,y,z)))),
last_valλ, INDEX(arrϑ,3,1),
HSTACK(last_valλ(1),last_valλ(2),last_valλ(3))
)I hope that helps. If you have any more questions, just let me know. ;)