While working through Fogus' Functional Javascript, I came across the trampoline function, which can be used to make safe recursive functions that don't blow up the stack. In Fogus' words, Of course there is no free lunch, even when using trampolines. While Ive managed to avoid exploding the call stack, Ive just transferred the problem to the heap.On top of moving this problem to the heap, the trampolined version of the function is slower. Is it possible to make a smart trampoline function that takes two forms of a function, a trampolined version and a non-trampolined version, and chooses (or predicts) the most efficient strategy?* Ref: https://github.com/funjs/book-source/blob/master/chapter06.js#L195*(or better yet combines both strategies (is this possible?) that uses fewer trampoline bounces, where each bounce is almost the maximum number of computations that can be performed recursively using the stack strategy)
Trampoline that automatically balances heap and stack
functional programming;recursion