Forum Discussion
Viz
Jan 05, 2021Brass Contributor
Lambda Example: Generate Fibonacci series
In this post, I would like to explain how I have used Lambda to create a function to generate a Fibonacci series array. This example can also be used to understand how to create an array where th...
PeterBartholomew1
Jan 12, 2021Silver Contributor
I hadn't realised there was a party going on here!!!
I am turning up late but I hope to be forgiven.
My version is similar but I think there is a difference in that I only accumulate the individual numbers from the sequence after the stack has been fully traversed, allowing the result array to be built after the return from each recursion.
= LAMBDA(r,n₀,Fᵣ₋₁,Fᵣ₋₂,
LET(
Fᵣ, Fᵣ₋₁ + Fᵣ₋₂,
seq, IF(r<n₀, FIB(r+1,n₀,Fᵣ,Fᵣ₋₁), 0),
IF(r=SEQUENCE(n₀), Fᵣ, seq) )
)(2,8,0,1)
To save the user from needing to initialise the recursion, I have used a wrapper function
= FIBONACCI(20)
= LAMBDA(n, FIB(2,n,0,1))(8)
as have others.