You can’t. #unfold() is not tail-recursive, it’s lazy. Your #fibs (and #tribs) variant isn’t tail-recursive either - the recursive call to #fibs() is not the outermost/final one (and there’s multiple calls to it, anyway). Tail recursion and laziness are distinct strategies for stack safety.
@sangamon I am very puzzled by your statement “Tail recursion and laziness are distinct strategies for stack safety.” Can you please expand or simply point me in right direction.
@yarchik assuming for a moment that you are not already thoroughly well versed in the subjects of tail recursion and laziness, here are two decks that you might find useful, the first one mainly, but not only, for tail recursion, and the second one for laziness. Apologies in advance if they are of no use to you (please download for original quality):
@yarchik please skip the first 19 slides of the first deck (at least on a first reading) - the key content is Sergei Winitzki’s great explanation of tail recursion.
@yarchik P.S. doh, scrap the above quote and sentence: they key content on tail recursion in the first deck is from slide 13 to 18, and yes, it is Sergei Winitzki’s great explanation of tail recursion.