Forum Discussion
Lambda Example: Generate Fibonacci series
In case of FIB() - yes, maybe, but the question is more general. How to find the compromise with error handling within lambdas which definitely could reduce the performance. Keep #VALUE! and #N/A as in native functions or add error handling and when. Decision is on case by case basis, I don't see common rule.
Another side effect is rounding which could give an error only on some inner iteration if use recursion, probably it will be bit hard to debug in some cases.
There may be a better solution but one approach I can think about handling error would be to have Lambda in two layers.
I am just putting the pseudo code here,
MAIN
=Lambda(n, IF(n<>int(n),"Enter integer",FIB(n)))
FIB
=Lambda(n, ..................)
I am new to recursive programming. So, I am not sure if this is the only solution. Perhaps better option exists.
- SergeiBaklanJan 07, 2021Diamond Contributor
The problem is that =n=INT(n) could return TRUE for the first iteration and FALSE on next ones if the difference is around 14th digit.
- lori_mJan 07, 2021Iron Contributor
In this case error handling could be carried out by wrapping in another function as suggested by Viz , the n = INT(n) condition is not iterated then. In general there will be the performance/error propagation considerations you mention so better to avoid adding extra conditions inside recursive functions if possible.
- SergeiBaklanJan 07, 2021Diamond Contributor
That's the question better to avoid or better not to avoid. I guess LAMBDA(), as well as simple LET(), don't improve the performance themselves compare to set of formulas and named cells/ranges, suspect they decrease it. What they improve that's flexibility and maintainability. From this point of view perhaps better not to avoid if only performance not becomes a critical issue for the concrete project. Again, I don't care about Fibonacci case, that's only an example.