How does this recursive function automatically convert to an iterative function?

I read tail recursion below

Tail recursion refers to a recursive call on the last line. A tail recursion can be mechanically eliminated by placing the body in a while and replacing the recursive call with one assignment per function argument.

for instance

void print(Iterator start, Iterator end, ostream& out=cout) { if(start == end) return; out << *start++ << endl; print(start, end, out); } 

converted to iterative according to the above specification as

 void print(Iterator start, Iterator end, ostream& out=cout) { while(true) { if(start == end) return; out << *start++ << endl; } } 

The passage above mentions that "replacing a recursive call with one assignment with a function argument, but in this example, we had no purpose?

Can someone explain and provide an example for a more detailed explanation of how to translate a recursive iterative function?

+4
source share
5 answers

The general transformation of recursive into iterative will look as follows.

Source:

 void print(Iterator start, Iterator end, ostream& out=cout) { if(start == end) return; out << *start++ << endl; print(start, end, out); } 

Converted Code:

 void print(Iterator start, Iterator end, ostream& out=cout) { while(true) { if(start == end) return; out << *start << endl; // One assignment per function argument for 'general' tail recursion start = start + 1; // (1) end = end; // (2) out = out; // (3) } } 

Three tasks, as in the explanation, are included. Assignment (1) is built into start++ , assignments (2) and (3) are optimized.

0
source

The destination is hidden in the increment operator:

 start++; 

is actually an assignment:

 start = start+1; 

Actually, the example (part one) is not very good.

 out << *start++ << endl; print(start, end, out); 

it should be

 out << *start << endl; print( start+1, end, out); 
+7
source

I do not think that it is important, whatever you say, it is important; just focus on the main problem where you want to convert a recursive function into a normal iterative function that can be performed (effortlessly) like,

 void print(Iterator start, Iterator end, ostream& out=cout) { while(start != end) { out << *start++ << endl; } } 
+6
source

It is a bit hidden in C ++, but start++ assigns a new value for each time in the loop.

+1
source

What they are talking about is that you assign the tail function call arguments to the parameter variables of this function call, but in this case it is not necessary, because you call the function with the same arguments (because, as others said, the change before the first argument to start occurred before the function was called).

Actually, to be precise, the iterative function should look like

 void print(Iterator start, Iterator end, ostream& out=cout) { while(true) { if(start == end) return; out << *start++ << endl; start = start; end = end; out = out; } } 

But these appointments are completely unnecessary, even if they are correct.

+1
source

Source: https://habr.com/ru/post/1369487/


All Articles