Trampolines and thunks
For the last solution to our problem, we shall have to think about the cause of the problem. Each pending recursive call creates a new entry stack. Whenever the stack gets too empty, the program crashes and our algorithm is history. So, if we can work out a way to avoid the stack growth, we should be free. The solution, in this case, is quite imposing and requires thunks and a trampoline—let's see what these are!
First, a thunk is really quite simple: it's just a nullary function (so, with no parameters) that helps delay a computation, providing a form of lazy evaluation. If you have a thunk, then, unless you call it, you won't get its value. For example, if you want to get the current date and time in ISO format, you ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access