Lambda Example: Generate Fibonacci series

%3CLINGO-SUB%20id%3D%22lingo-sub-2034565%22%20slang%3D%22en-US%22%3ELambda%20Example%3A%20Generate%20Fibonacci%20series%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2034565%22%20slang%3D%22en-US%22%3E%3CP%3EIn%20this%20post%2C%20I%20would%20like%20to%20explain%20how%20I%20have%20used%20Lambda%20to%20create%20a%20function%20to%20generate%20a%20Fibonacci%20series%20array.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20example%20can%20also%20be%20used%20to%20understand%20how%20to%20create%20an%20array%20where%20the%20value%20of%20an%20element%20in%20the%20array%20depends%20on%20previous%20elements%20in%20the%20same%20array.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Fibonacci%20series.gif%22%20style%3D%22width%3A%20363px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F244405i25B1CC64A8F5F448%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Fibonacci%20series.gif%22%20alt%3D%22Generating%20Fibonacci%20as%20a%20dynamic%20array%20by%20%40Viz%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EGenerating%20Fibonacci%20as%20a%20dynamic%20array%20by%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F530769%22%20target%3D%22_blank%22%3E%40Viz%3C%2FA%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20function%20has%20been%20created%20using%20three%20function%20in%20two%20layers.%20The%20inner%20layer%20functions%20include%20the%20following%3A%3C%2FP%3E%3CP%3E%3CSTRONG%3EInFib%3C%2FSTRONG%3E%3A%20This%20function%20generates%20the%20Nth%20Fibonacci%20number%3C%2FP%3E%3CP%3E%3CSTRONG%3EInFibSer%3A%26nbsp%3B%3C%2FSTRONG%3EThis%20function%20generates%20the%20entire%20Fibonacci%20series%20up%20to%20the%20Nth%20number%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20two%20functions%20mentioned%20above%20require%20arguments%20that%20are%20complicated%20and%20less%20intuitive.%20Therefore%2C%20we%20use%20an%20outer%20layer%20function%20(%3CSTRONG%3EFibSeries%3C%2FSTRONG%3E)%26nbsp%3Bthat%20generates%20the%20necessary%20argument%20and%20calls%20the%20two%20functions%20to%20generate%20the%20series.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%225%22%3E%3CSTRONG%3E%3DInFIB%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%3CP%3E'This%20function%20returns%20nth%20Fibonacci%20using%20tail%20recursion%3CBR%20%2F%3E'It%20takes%20three%20arguments%20n%2C%20a%3D0%20%2C%20b%3D1%20as%20argument%20and%20returns%20the%20nth%20Fibonacci%20value%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%3DLAMBDA(n%2Ca%2Cb%2CIF(n%3D1%2Ca%2CIF(n%3D2%2Cb%2CInfib(n-1%2Cb%2Ca%2Bb))))%0A%2F*%20The%20value%20for%20the%20arguments%20a%20and%20b%20should%20be%20passed%20as%200%20and%201%2C%20respectively.%20*%2F%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EExample%3A%3C%2FP%3E%3CP%3E%3DInFIB(10%2C0%2C1)%20would%20generate%2034%20as%20the%20value%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOur%20objective%2C%20here%2C%20is%20not%20to%20obtain%20a%20single%20value%20but%20to%20generate%20an%20entire%20array.%20However%2C%20here%20is%20where%20we%20have%20a%20challenge%3A%20While%20it%20is%20easier%20to%20manipulate%20an%20existing%20array%2C%20it%20is%20far%20more%20to%20difficult%20to%20create%20a%20new%20array%20(other%20than%20sequence%20array)%20or%20extend%20an%20existing%20array.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETherefore%2C%20in%20order%20to%20create%20a%20Fibonacci%20series%2C%20we%20are%20going%20to%20pass%20a%20dummy%20array%20of%20n%20values.%20This%20array%20will%20have%200%20as%20the%20first%20element%20and%201%20as%20the%20value%20for%20all%20the%20other%20elements.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20following%20image%20portrays%20the%20recursive%20addition%20process%20that%20converts%20that%20the%20initial%20array%20into%20a%20Fibonacci%20series.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Fibonacci%20algo%20illustration.png%22%20style%3D%22width%3A%20369px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F244409iB736F209A3930A60%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Fibonacci%20algo%20illustration.png%22%20alt%3D%22Fibonacci%20algo%20illustration.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20following%20code%20does%20carries%20out%20the%20process%20recursively%20till%20the%20maximum%20value%20of%20the%20array%20equals%20the%20nth%20Fibonacci%20value.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%225%22%3E%3CSTRONG%3E%3DINFIBSER%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23339966%22%3E'This%20inner%20function%20takes%20three%20arguments%20and%20returns%20a%20Fibonacci%26nbsp%3Bseries%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23339966%22%3E'The%20three%20arguments%20are%20nth%20Fibonacci%26nbsp%3Bvalue%2C%20the%20initial%20array%20and%20a%20sequence%20array%20(sequential%20numbers%20from%201%20to%20n)%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23339966%22%3E'Note%2C%20the%20sequence%20array%20can%20instead%20be%20generated%20inside%20the%20function%20but%20it%20may%20inefficient%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%3DLambda(NthFib%2C%20Initval%2C%20Sqn%2C%0A%20Let(%0A%20%20%20%20%20Maxval%2CMax(Initval)%2C%0A%20%20%20%20%20If(Maxval%20%3D%20NthFib%2C%0A%20%20Initval%2C%0A%20%20Let(%0A%20%20%20%20%20%20NewVal%2CIf(Maxval%3D1%2CInitval%2B1*(Sqn%26gt%3B%3D4)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Let(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20StPos%2CMatch(Maxval%2CInitval%2C0)%2B1%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20adval%2CLarge(unique(Initval)%2C2)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20Initval%2Badval*(Sqn%26gt%3B%3DStPos)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20InFibser(NthFib%2CNewVal%2CSqn)%0A%20%20)%0A%20)%0A)%0A%20)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20the%20first%20iteration%2C%20the%20above%20function%20adds%20one%20to%20all%20the%20values%20from%20the%20fourth%20position%20onwards.%20For%20the%20subsequent%20iterations%20it%20adds%20the%20second%20largest%20unique%20value%20to%20all%20values%20that%20all%202%20places%20after%20the%20position%20of%20such%20second%20largest%20value.%20The%20above%20function%20uses%20the%20match%20function%20to%20identify%20the%20position%20pointer.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAlthough%20the%20above%20function%20can%20generate%20the%20Fibonacci%20series%2C%20as%20mentioned%20earlier%2C%20it%20requires%20arguments%20that%20are%20less%20intuitive%20and%20which%20requires%20some%20set%20up.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETherefore%2C%20here%20we%20have%20an%20outer%20layer%20function%20that%20requires%20only%20one%20argument%2C%20n.%20It%20creates%20the%20necessary%20argument%20to%20be%20passed%20to%20the%20inner%20layer%20function%20and%20calls%20them.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3E%3DFIBSERIES%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23339966%22%3E'This%20outermost%20function%20generates%20an%20initial%20array%2C%20a%20sequence%20array%2C%20and%20nth%20fibonnaci%20value%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23339966%22%3E'It%20then%20calls%20the%20inner%20functions%20with%20these%20parameters%26nbsp%3B%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%3DLambda(n%2C%0A%20Let(%0A%20%20Sqn%2Csequence(n)%2C%0A%20%20initval%2Csign(Sqn-1)%2C%0A%20%20Nthfib%2CInfib(n%2C0%2C1)%2C%0A%20%20InFibser(Nthfib%2C%20Initval%2C%20Sqn)%0A%20)%0A%20)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20final%20function%20can%20be%20used%20by%20the%20user%20to%20generate%20the%20Fibonacci%20series%20by%20merely%20passing%20the%20number%20of%20values%20required%20as%20argument.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHope%20you%20found%20this%20useful.%20I%20would%20love%20to%20know%20if%20you%20think%20there%20is%20a%20way%20to%20improve%20the%20algorithm%2C%20here.%20I%20am%20especially%20looking%20for%20suggestions%20that%20could%20replace%20the%20match%20%2F%20xmatch%20in%20the%20InFibSer%20function%20to%20get%20the%20position%20pointer.%20Since%20the%20positions%20move%20sequentially%2C%20I%20believe%20a%20mathematical%20solution%20may%20exist.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2034565%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2035685%22%20slang%3D%22en-US%22%3ERe%3A%20Lambda%20Example%3A%20Generate%20Fibonacci%20series%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2035685%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F530769%22%20target%3D%22_blank%22%3E%40Viz%3C%2FA%3E%26nbsp%3B%20I%20should%20have%20replied%20here%20instead%20of%20the%20other%20post%20for%20further%20discussion.%3C%2FP%3E%3CP%3EHave%20also%20tweaked%20recursive%20formula%20for%20the%20n%3D1%20case.%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3EFIB%3A%0A%3DLAMBDA(n%2C%0A%20%20%20%20%20%20%20IF(n%26lt%3B%3D2%2C%0A%20%20%20%20%20%20%20SEQUENCE(n%2C%2C0)%2C%0A%20%20%20%20%20%20%20LET(b%2CFIB(n-1)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20IF(SEQUENCE(n)%3CN%3E%3C%2FN%3E%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2035945%22%20slang%3D%22en-US%22%3ERe%3A%20Lambda%20Example%3A%20Generate%20Fibonacci%20series%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2035945%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F288074%22%20target%3D%22_blank%22%3E%40lori_m%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20add%20error%20handling%20as%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-excel%22%3E%3CCODE%3E%3DLAMBDA(n%2C%0A%20%20%20%20%20%20%20IF(n%26lt%3B%26gt%3BINT(n)%2C%22use%20integer%20as%20argument%22%2C%0A%20%20%20%20%20%20%20IF(n%26lt%3B%3D2%2C%0A%20%20%20%20%20%20%20SEQUENCE(n%2C%2C0)%2C%0A%20%20%20%20%20%20%20LET(b%2CFIB(n-1)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20IF(SEQUENCE(n)%3CN%3E%3C%2FN%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3Eit%20returns%20errors%20message%20as%20expected%20for%20FIB(10.1)%2C%20but%20the%20spill%20as%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22image.png%22%20style%3D%22width%3A%20148px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F244524i08DF1DBCB04224DE%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22image.png%22%20alt%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3Efor%20FIB(10%2B1e-14).%20Correction%20could%20be%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-excel%22%3E%3CCODE%3E%3DLAMBDA(n%2C%0A%20%20%20IF(n%26lt%3B%26gt%3BINT(n)%2C%22use%20integer%20as%20argument%22%2C%0A%20%20%20%20%20%20LET(m%2C%20INT(n)%2C%0A%20%20%20%20%20%20%20%20%20IF(m%26lt%3B%3D2%2C%0A%20%20%20%20%20%20%20%20%20SEQUENCE(m%2C%2C0)%2C%0A%20%20%20%20%20%20%20%20%20LET(b%2CFIB(m-1)%2C%0A%20%20%20%20%20%20%20%20%20IF(SEQUENCE(m)%3CM%3E%3C%2FM%3E%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2036082%22%20slang%3D%22en-US%22%3ERe%3A%20Lambda%20Example%3A%20Generate%20Fibonacci%20series%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2036082%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F288074%22%20target%3D%22_blank%22%3E%40lori_m%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWow!%20your%20solution%20is%20super%20elegant!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20was%20initially%20trying%20something%20closer%20to%20your%20first%20solution%20but%20I%20could%20not%20crack%20the%20logic%20to%20extend%20the%20size%20of%20array.%20And%20the%20algorithm%20I%20used%20to%20extend%20the%20array%20made%20the%20function%20very%20slow.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20still%20getting%20my%20head%20around%20your%20function%20to%20understand%20how%20the%20array%20is%20getting%20extended.%20But%20thank%20you%20very%20much%20for%20sharing%20these%20solutions.%20It%20was%20very%20useful.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2037287%22%20slang%3D%22en-US%22%3ERe%3A%20Lambda%20Example%3A%20Generate%20Fibonacci%20series%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2037287%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F521%22%20target%3D%22_blank%22%3E%40Sergei%20Baklan%3C%2FA%3E%26nbsp%3BGood%20point.%20I%20think%20FIB%20should%20probably%20truncate%20decimals%20to%20integers%20if%20entered%20as%20parameters%20to%20be%20consistent%20with%20other%20functions.%20It%20seems%20not%20much%20change%20is%20required%20to%20do%20that%20because%20SEQUENCE%20and%20INDEX%20functions%20already%20do%20this%20(though%20to%20be%20precise%20one%20should%20check%20values%20very%20close%20to%20integer%20as%20you%20say.)%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel%22%3E%3CCODE%3E%3DLAMBDA(n%2C%0A%20%20%20%20%20%20%20%20IF(n%26lt%3B3%2C%0A%20%20%20%20%20%20%20%20SEQUENCE(n%2C%2C0)%2C%0A%20%20%20%20%20%20%20%20LET(b%2CFIB(n-1)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20IF(SEQUENCE(n)%3CINT%3E%3C%2FINT%3E%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F530769%22%20target%3D%22_blank%22%3E%40Viz%3C%2FA%3E%26nbsp%3B%20I%20found%20this%20pretty%20challenging%20and%20don't%20find%26nbsp%3Brecursion%20very%20intuitive%20either.%20I%20first%20laid%20out%20the%20expected%20results%20for%20all%20input%20values%20from%201%20to%2010%20horizontally%20referring%20to%20prior%20results%20using%20spill%20operator%20and%20then%20substituted%20LET%20to%20make%20it%20more%20efficient.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2039336%22%20slang%3D%22en-US%22%3ERe%3A%20Lambda%20Example%3A%20Generate%20Fibonacci%20series%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2039336%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F288074%22%20target%3D%22_blank%22%3E%40lori_m%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20case%20of%20FIB()%20-%20yes%2C%20maybe%2C%20but%20the%20question%20is%20more%20general.%20How%20to%20find%20the%20compromise%20with%20error%20handling%20within%20lambdas%20which%20definitely%20could%20reduce%20the%20performance.%20Keep%20%23VALUE!%20and%20%23N%2FA%20as%20in%20native%20functions%20or%20add%20error%20handling%20and%20when.%20Decision%20is%20on%20case%20by%20case%20basis%2C%20I%20don't%20see%20common%20rule.%3C%2FP%3E%0A%3CP%3EAnother%20side%20effect%20is%20rounding%20which%20could%20give%20an%20error%20only%20on%20some%20inner%20iteration%20if%20use%20recursion%2C%20probably%20it%20will%20be%20bit%20hard%20to%20debug%20in%20some%20cases.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2039996%22%20slang%3D%22en-US%22%3ERe%3A%20Lambda%20Example%3A%20Generate%20Fibonacci%20series%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2039996%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F521%22%20target%3D%22_blank%22%3E%40Sergei%20Baklan%3C%2FA%3E%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F288074%22%20target%3D%22_blank%22%3E%40lori_m%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThere%20may%20be%20a%20better%20solution%20but%20one%20approach%20I%20can%20think%20about%20handling%20error%20would%20be%20to%20have%20Lambda%20in%20two%20layers.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20just%20putting%20the%20pseudo%20code%20here%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMAIN%3C%2FP%3E%3CP%3E%3DLambda(n%2C%20IF(n%26lt%3B%26gt%3Bint(n)%2C%22Enter%20integer%22%2CFIB(n)))%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFIB%3C%2FP%3E%3CP%3E%3DLambda(n%2C%20..................)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20new%20to%20recursive%20programming.%20So%2C%20I%20am%20not%20sure%20if%20this%20is%20the%20only%20solution.%20Perhaps%20better%20option%20exists.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2040037%22%20slang%3D%22en-US%22%3ERe%3A%20Lambda%20Example%3A%20Generate%20Fibonacci%20series%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2040037%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F530769%22%20target%3D%22_blank%22%3E%40Viz%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20problem%20is%20that%20%3Dn%3DINT(n)%20could%20return%20TRUE%20for%20the%20first%20iteration%20and%20FALSE%20on%20next%20ones%20if%20the%20difference%20is%20around%2014th%20digit.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2040613%22%20slang%3D%22en-US%22%3ERe%3A%20Lambda%20Example%3A%20Generate%20Fibonacci%20series%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2040613%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F521%22%20target%3D%22_blank%22%3E%40Sergei%20Baklan%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20this%20case%20error%20handling%20could%20be%20carried%20out%20by%20wrapping%20in%20another%20function%20as%20suggested%20by%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F530769%22%20target%3D%22_blank%22%3E%40Viz%3C%2FA%3E%26nbsp%3B%2C%20the%20n%20%3D%20INT(n)%20condition%20is%20not%20iterated%20then.%20In%20general%20there%20will%20be%20the%20performance%2Ferror%20propagation%20considerations%20you%20mention%20so%20better%20to%20avoid%20adding%20extra%20conditions%20inside%20recursive%20functions%20if%20possible.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2040656%22%20slang%3D%22en-US%22%3ERe%3A%20Lambda%20Example%3A%20Generate%20Fibonacci%20series%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2040656%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F288074%22%20target%3D%22_blank%22%3E%40lori_m%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThat's%20the%20question%20better%20to%20avoid%20or%20better%20not%20to%20avoid.%20I%20guess%20LAMBDA()%2C%20as%20well%20as%20simple%20LET()%2C%20don't%20improve%20the%20performance%20themselves%20compare%20to%20set%20of%20formulas%20and%20named%20cells%2Franges%2C%20suspect%20they%20decrease%20it.%20What%20they%20improve%20that's%20flexibility%20and%20maintainability.%20From%20this%20point%20of%20view%20perhaps%20better%20not%20to%20avoid%20if%20only%20performance%20not%20becomes%20a%20critical%20issue%20for%20the%20concrete%20project.%20Again%2C%20I%20don't%20care%20about%20Fibonacci%20case%2C%20that's%20only%20an%20example.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2043780%22%20slang%3D%22en-US%22%3ERe%3A%20Lambda%20Example%3A%20Generate%20Fibonacci%20series%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2043780%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F521%22%20target%3D%22_blank%22%3E%40Sergei%20Baklan%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20suspect%20you%20are%20right%20that%20while%20LAMBDA%20%2F%20LET%20solutions%20may%20add%20flexibility%20and%20maintainability%20they%20may%20often%20be%20a%20little%20less%20efficient%20than%20more%20traditional%20solutions%20involving%20helper%20ranges.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFor%20discussion%20it's%20helpful%20to%20have%20some%20concrete%20examples%20in%20mind.%20Many%20common%20problem%20scenarios%20involving%20manipulating%20data%20tables%20in%20various%20ways%20could%20be%20implemented%20using%20DA%20methods%20without%20the%20need%20for%20recursion%20in%20which%20case%20inserting%20extra%20conditions%20is%20not%20much%20of%20an%20issue%20particularly%20if%20these%20conditions%20can%20be%20hidden%20from%20view%20inside%20LAMBDA%20so%20as%20not%20to%20be%20a%20source%20of%20distraction.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOne%20broad%20class%20of%20problem%20where%20recursion%20would%20generally%20be%20required%20are%20formula%20solvers.%20For%20financial%20models%20obvious%20examples%20include%20yield-to-maturity%2C%20implied%20volatility%20and%20bootstrapping%20(curve%20fitting)%20calculations.%20Here%20the%20objective%20is%20to%20find%20a%20solution%20to%20a%20model%20which%20is%20satisfied%20within%20a%20given%20bound.%20IMHO%20an%20idealistic%20approach%20for%20these%20type%20of%20cases%20would%20keep%20the%20recursive%20code%20compact%20so%20it%20can%20be%20easily%20translated%20into%20business%20logic%20and%20use%20wrapper%20functions%20for%20implementing%20any%20additional%20conditions%20as%20suggested%20above.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2054841%22%20slang%3D%22en-US%22%3ERe%3A%20Lambda%20Example%3A%20Generate%20Fibonacci%20series%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2054841%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F530769%22%20target%3D%22_blank%22%3E%40Viz%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20hadn't%20realised%20there%20was%20a%20party%20going%20on%20here!!!%3C%2FP%3E%3CP%3EI%20am%20turning%20up%20late%20but%20I%20hope%20to%20be%20forgiven.%3C%2FP%3E%3CP%3EMy%20version%20is%20similar%20but%20I%20think%20there%20is%20a%20difference%20in%20that%20I%20only%20accumulate%20the%20individual%20numbers%20from%20the%20sequence%20after%20the%20stack%20has%20been%20fully%20traversed%2C%20allowing%20the%20result%20array%20to%20be%20built%20after%20the%20return%20from%20each%20recursion.%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel%22%3E%3CCODE%3E%3D%20LAMBDA(r%2Cn%E2%82%80%2CF%E1%B5%A3%E2%82%8B%E2%82%81%2CF%E1%B5%A3%E2%82%8B%E2%82%82%2C%0A%20%20LET(%0A%20%20%20%20%20%20F%E1%B5%A3%2C%20F%E1%B5%A3%E2%82%8B%E2%82%81%20%2B%20F%E1%B5%A3%E2%82%8B%E2%82%82%2C%0A%20%20%20%20%20%20seq%2C%20IF(r%3CN%3E%3C%2FN%3E%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3ETo%20save%20the%20user%20from%20needing%20to%20initialise%20the%20recursion%2C%20I%20have%20used%20a%20wrapper%20function%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel%22%3E%3CCODE%3E%3D%20FIBONACCI(20)%0A%0A%3D%20LAMBDA(n%2C%20FIB(2%2Cn%2C0%2C1))(8)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3Eas%20have%20others.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22image.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F245936i28F0D59F4BA44160%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22image.png%22%20alt%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2071137%22%20slang%3D%22en-US%22%3ERe%3A%20Lambda%20Example%3A%20Generate%20Fibonacci%20series%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2071137%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F288074%22%20target%3D%22_blank%22%3E%40lori_m%3C%2FA%3EWas%20struggling%20to%20figure%20out%20how%20to%20extend%20an%20array%20as%20well%2C%20thanks%20for%20this%20example.%20Using%20your%20FIB%20array%20extension%20as%20a%20hint%20I've%20got%20a%20generic%20iterator%20layout%20after%20struggling%20with%20the%20formula%20a%20bit.%20Formula%20was%20returning%20a%20%23NUM%20error%2C%20and%20figured%20out%20that%20TYPE(INDEX(Seed%2CROWS(Seed)))%3D64%20(array)%20rather%20than%201%20for%20number.%20Any%20ideas%20on%20better%20way%20to%20do%20this%3F%20Decided%20to%20work%20through%20Project%20Euler%20problems%20to%20wrap%20my%20head%20around%20LAMBDA%20usage%20and%20this%20is%20part%20of%20my%20solution%20to%20question%20on%20Collatz%20sequence.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3DLET(n%2C%3CCELL_REF%20or%3D%22%22%20hardcode%3D%22%22%20number%3D%22%22%3E%2C%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20LET(Next%2CLAMBDA(Prior%2CIF(ISEVEN(Prior)%2CPrior%2F2%2CPrior*3%2B1))%2C%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Seed%2CSEQUENCE(1%2C1%2Cn%2C0)%2C%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Iterate%2CLAMBDA(ME%2CSeed%2C%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20LET(PriorVal%2C%3CSTRONG%3ENUMBERVALUE(ARRAYTOTEXT(INDEX(Seed%2CROWS(Seed))))%3C%2FSTRONG%3E%2C%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20CurrVal%2CNext(PriorVal)%2C%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20CurrResult%2CIF(SEQUENCE(ROWS(Seed)%2B1)%26lt%3B(ROWS(Seed)%2B1)%2CSeed%2CCurrVal)%2C%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20IF(PriorVal%3D1%2CSeed%2CIF(CurrVal%3D1%2CCurrResult%2CME(ME%2CCurrResult)))%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20)%2C%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Iterate(Iterate%2CSeed)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20)%3CBR%20%2F%3E)%3C%2FCELL_REF%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

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 the value of an element in the array depends on previous elements in the same array.

 

Generating Fibonacci as a dynamic array by @VizGenerating Fibonacci as a dynamic array by @Viz

 

This function has been created using three function in two layers. The inner layer functions include the following:

InFib: This function generates the Nth Fibonacci number

InFibSer: This function generates the entire Fibonacci series up to the Nth number

 

The two functions mentioned above require arguments that are complicated and less intuitive. Therefore, we use an outer layer function (FibSeries) that generates the necessary argument and calls the two functions to generate the series.

 

=InFIB

'This function returns nth Fibonacci using tail recursion
'It takes three arguments n, a=0 , b=1 as argument and returns the nth Fibonacci value

 

 

=LAMBDA(n,a,b,IF(n=1,a,IF(n=2,b,Infib(n-1,b,a+b))))
/* The value for the arguments a and b should be passed as 0 and 1, respectively. */

 

 

 

Example:

=InFIB(10,0,1) would generate 34 as the value

 

Our objective, here, is not to obtain a single value but to generate an entire array. However, here is where we have a challenge: While it is easier to manipulate an existing array, it is far more to difficult to create a new array (other than sequence array) or extend an existing array.

 

Therefore, in order to create a Fibonacci series, we are going to pass a dummy array of n values. This array will have 0 as the first element and 1 as the value for all the other elements.

 

The following image portrays the recursive addition process that converts that the initial array into a Fibonacci series.

 

Fibonacci algo illustration.png

 

The following code does carries out the process recursively till the maximum value of the array equals the nth Fibonacci value.

 

=INFIBSER

'This inner function takes three arguments and returns a Fibonacci series
'The three arguments are nth Fibonacci value, the initial array and a sequence array (sequential numbers from 1 to n)
'Note, the sequence array can instead be generated inside the function but it may inefficient

 

 

=Lambda(NthFib, Initval, Sqn,
	Let(
	    Maxval,Max(Initval),
	    If(Maxval = NthFib,
		Initval,
		Let(
		    NewVal,If(Maxval=1,Initval+1*(Sqn>=4),
		              Let(
			          StPos,Match(Maxval,Initval,0)+1,
			          adval,Large(unique(Initval),2),
			          Initval+adval*(Sqn>=StPos)
		                  )
			     ),
		    InFibser(NthFib,NewVal,Sqn)
		)
	)
)
 )

 

 

 

In the first iteration, the above function adds one to all the values from the fourth position onwards. For the subsequent iterations it adds the second largest unique value to all values that all 2 places after the position of such second largest value. The above function uses the match function to identify the position pointer.

 

Although the above function can generate the Fibonacci series, as mentioned earlier, it requires arguments that are less intuitive and which requires some set up.

 

Therefore, here we have an outer layer function that requires only one argument, n. It creates the necessary argument to be passed to the inner layer function and calls them.

 

=FIBSERIES

'This outermost function generates an initial array, a sequence array, and nth fibonnaci value
'It then calls the inner functions with these parameters 

 

 

 

=Lambda(n,
	Let(
		Sqn,sequence(n),
		initval,sign(Sqn-1),
		Nthfib,Infib(n,0,1),
		InFibser(Nthfib, Initval, Sqn)
	)
 )

 

 

 

This final function can be used by the user to generate the Fibonacci series by merely passing the number of values required as argument.

 

Hope you found this useful. I would love to know if you think there is a way to improve the algorithm, here. I am especially looking for suggestions that could replace the match / xmatch in the InFibSer function to get the position pointer. Since the positions move sequentially, I believe a mathematical solution may exist.

 

Thank you

18 Replies

@Viz  I should have replied here instead of the other post for further discussion.

Have also tweaked recursive formula for the n=1 case.

FIB:
=LAMBDA(n,
       IF(n<=2,
       SEQUENCE(n,,0),
       LET(b,FIB(n-1),
            IF(SEQUENCE(n)<n,b,INDEX(b,n-1)+INDEX(b,n-2))
       )))

 

@lori_m 

If add error handling as

=LAMBDA(n,
       IF(n<>INT(n),"use integer as argument",
       IF(n<=2,
       SEQUENCE(n,,0),
       LET(b,FIB(n-1),
            IF(SEQUENCE(n)<n,b,INDEX(b,n-1)+INDEX(b,n-2))
       ))))

it returns errors message as expected for FIB(10.1), but the spill as

image.png

for FIB(10+1e-14). Correction could be

=LAMBDA(n,
   IF(n<>INT(n),"use integer as argument",
      LET(m, INT(n),
         IF(m<=2,
         SEQUENCE(m,,0),
         LET(b,FIB(m-1),
         IF(SEQUENCE(m)<m,b,INDEX(b,m-1)+INDEX(b,m-2))
   )))))

@lori_m 

 

Wow! your solution is super elegant!

 

I was initially trying something closer to your first solution but I could not crack the logic to extend the size of array. And the algorithm I used to extend the array made the function very slow.

 

I am still getting my head around your function to understand how the array is getting extended. But thank you very much for sharing these solutions. It was very useful.

@Sergei Baklan Good point. I think FIB should probably truncate decimals to integers if entered as parameters to be consistent with other functions. It seems not much change is required to do that because SEQUENCE and INDEX functions already do this (though to be precise one should check values very close to integer as you say.)

=LAMBDA(n,
        IF(n<3,
        SEQUENCE(n,,0),
        LET(b,FIB(n-1),
             IF(SEQUENCE(n)<INT(n),b,INDEX(b,n-1)+INDEX(b,n-2)))
       ))

 

@Viz  I found this pretty challenging and don't find recursion very intuitive either. I first laid out the expected results for all input values from 1 to 10 horizontally referring to prior results using spill operator and then substituted LET to make it more efficient.

@lori_m 

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.

@Sergei Baklan @lori_m 

 

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.

@Viz 

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.

@Sergei Baklan 

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.

@lori_m 

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.

@Sergei Baklan 

I suspect you are right that while LAMBDA / LET solutions may add flexibility and maintainability they may often be a little less efficient than more traditional solutions involving helper ranges.

 

For discussion it's helpful to have some concrete examples in mind. Many common problem scenarios involving manipulating data tables in various ways could be implemented using DA methods without the need for recursion in which case inserting extra conditions is not much of an issue particularly if these conditions can be hidden from view inside LAMBDA so as not to be a source of distraction.

 

One broad class of problem where recursion would generally be required are formula solvers. For financial models obvious examples include yield-to-maturity, implied volatility and bootstrapping (curve fitting) calculations. Here the objective is to find a solution to a model which is satisfied within a given bound. IMHO an idealistic approach for these type of cases would keep the recursive code compact so it can be easily translated into business logic and use wrapper functions for implementing any additional conditions as suggested above.

@Viz 

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.

image.png

@lori_mWas struggling to figure out how to extend an array as well, thanks for this example. Using your FIB array extension as a hint I've got a generic iterator layout after struggling with the formula a bit. Formula was returning a #NUM error, and figured out that TYPE(INDEX(Seed,ROWS(Seed)))=64 (array) rather than 1 for number. Any ideas on better way to do this? Decided to work through Project Euler problems to wrap my head around LAMBDA usage and this is part of my solution to question on Collatz sequence.

 

=LET(n,<cell_ref or hardcode number>,
         LET(Next,LAMBDA(Prior,IF(ISEVEN(Prior),Prior/2,Prior*3+1)),
                Seed,SEQUENCE(1,1,n,0),
                Iterate,LAMBDA(ME,Seed,
                           LET(PriorVal,NUMBERVALUE(ARRAYTOTEXT(INDEX(Seed,ROWS(Seed)))),
                                 CurrVal,Next(PriorVal),
                                 CurrResult,IF(SEQUENCE(ROWS(Seed)+1)<(ROWS(Seed)+1),Seed,CurrVal),
                                 IF(PriorVal=1,Seed,IF(CurrVal=1,CurrResult,ME(ME,CurrResult)))
                           )
                 ),
                Iterate(Iterate,Seed)
         )
)

@tboulden 

I have still to get to grips with your formula; I find recursive formulae especially opaque!

One observation, though.  It is possible to conduct the recursion, and maintain a counter that could be used as a backup termination criterion, without building the array as you go.

When the end point is reached, all the values obtained along the way are still present within the memory stack and are accessible one at a time following return from the previous level.  In the Fibonacci example I used this to combine the current value and the return array to build the complete series as an array of known length.

@Peter Bartholomew 

It's known that recursive lambda allows quite limited number of iteration. I tried to play with different variants of FIB() presented to find max allowed parameter for each. Actually that's about number of iteration, we are within max returned number limit.

image.png

In last case I just added text constant within LET().

That's not an analysis, just stats. 

Thanks Peter, I reviewed your version, and translated it for my case; I'll have to experiment with it some, but for this particular problem, I run up against the stack depth (about 170ish recursions deep with the parameter count/setup I'm using). I'm pleased that with your version, I avoid the NUMBERVALUE(ARRAYTOTEXT(INDEX(...))) usage, though I imagine there is probably a better way to handle that as well. Thanks again!

PS: Part of wrapping my head around this is has been this useful post: https://www.sumproduct.com/news/article/lambda-formulaic-recursion-its-all-about-me

@tboulden 

Your Collatz example looks intriguing I will try and make time to look. And thanks for that useful link; it is stated near the end that: 

 

"do note that the current operand stack limit in Excel is 1,024.  This should be borne in mind together with calculation times, as the current recursion limit is set as 1,024 divided by (number of lambda parameters + 1)."

 

The 1024 stack limit does seem to tally with Sergei's results further up. It appears that the figures all roughly divide 1024 (within a few units of rounding):

341 ~ 3 stack operands per call

256 ~ 4 stack operands per call

146 ~ 7 stack operands per call

This also suggests other parameters may need to be taken into account in addition to lambda parameters.

@Viz et al

Thanks for this discussion.  It has been useful in taking me from the point where I didn't know there was a limit on the level of recursion achievable to actually acquiring some understanding of the factors involved.

@tboulden 

I am pleased the exchange of workbooks has delivered an idea that may be useful to you.  Thanks for introducing me to the Collatz sequence, it proved interesting.  I modified my Fibonacci workbook to implement the sequence which served to reinforce what I think of as 'the anatomy of a recursive Lambda function.  I have yet to achieve a consistent development and testing strategy though.

 

I modified the Macro that uploads Lambda formulas from the worksheet to allow the option of cancelling out and so leaving the Lambda definition unaltered (more accurately, to copy the formula before deleting it, so that it can be reinstated if the changes cause the formula to fail.  I have also included a small Lambda function to collect the scalar values returned, last first, from the recursion stack as an array.  

 

I have yet to decide whether Lambdas are a novelty, (to be brought out to show off or at times of extreme need) or whether they provide the basic building blocks of future Excel solutions.

 

image.png

Peter, you surely know how to make a nice presentation! Will take some time and review what you've set up here. Thanks very much!