For the last key 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 your 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 sonorous 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 for a form of lazy evaluation. If you have a thunk, 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 ...