Forum Discussion
FIFO Inventory Formula Challenge
Hi Lori
It shouldn't take you more than a few minutes to write a recursive Python script to reduce any LET function back to its simplest form (grin)! Whether the resulting formula is 'nice' or not is a matter of taste. Like beauty, simplicity is in the eye of the beholder. What others see as simplicity I see as primitive and unstructured, i.e. a classic demonstration of complexity!
BTW I reported the behaviour of my recursive formula that produced an array of results for the 5 products to Microsoft as an error. Oddly, although I could correctly output the array, I could not then count or sum the values it held (the start of the problem was that the 5 values were represented within 1 row?
Agree discussions around simplicity / complexity, etc. are pretty pointless given their subjective (and recursive!) nature. I was thinking of 'Nice' in a 'Pythonic' sense and it does seem Excel is heading in the direction of such languages.
I don't have access to LAMBDA to test but it should be possible to use recursion within a LET statement by doing something along the lines of this Python code:
Y = lambda f: (lambda x: x(x))(lambda y: f(lambda *args: y(y)(*args)))
fib = lambda f: lambda n: (n if n<2 else f(n-1)+f(n-2))
>>> list(map(Y(fib),range(10)))
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
- PeterBartholomew1Feb 12, 2021Silver Contributor
I wouldn't say that I have reached the point of being able to read the concise syntax that seems to be favoured in the world of functional analysis with any degree of fluency! Even in the world of imperative computing I always preferred verbose to concise (Visual Basic to Java). The Y combinator still leaves me struggling to read what is the function definition and what arguments are being processed. With conventional math, which is somewhat concise, I instinctively feed in phases such as 'with respect to' but it has yet to happen with Curry's notation.
Despite that, I seem to have written a recursive Lambda function that I have applied to Twifoo 's challenge problem and then reapplied it to a simpler problem (that of calculating a 1D column that represents row totals of a 2D array) without change.
It might be that FOREACH implements the Y combinator and XSTACK the MAP function but, right now, I lack the clarity of thought to be sure. For the sake of completeness, my function to sum rows was
= LAMBDA(arr,num, SUM(INDEX(array,num,0)) )
Not totally awe inspiring, but useful.
- PeterBartholomew1Feb 12, 2021Silver Contributor
I forgot to say, the function call was
= FOREACH(ROWS(array), SUM1ROW, array)
The first parameter would appear to be superfluous but the variable it initialises served as a counter in order to terminate the recursion.
BTW the formula you wrote for Currying the flow within the accumulation works (I assumed you had been in a position to test it). Whether it serves the intended purpose of reducing memory demands or parameter count, I have yet to determine.