making a procedure tail-recursive: strategy
steps
- consider the recursive process, and ask yourself:
at some point while computation is still being deferred,
what progress might have been achieved with the values produced so far?
- now introduce
a formal parameter (or several) whose value will be the result of that progress
an expression that computes the progress from
- the value of the formal parameter (the progress up to the last call)
- the values produced prior to the recursive call
- determine the termination condition
an expression using the existing arguments (and perhaps the new formal parameter)
- determine a value to represent progress at the start
this becomes the argument to the outermost call
- transcribe into code
- wrap in another procedure without the extra parameter