When writing a string backwards recursively, there are at least two
ways to do it:

in pseudocode:

void writeBackward1 ( string s):
   if s is empty
     do nothing
   if s isn't empty
     output last character of s
     writeBackward1 ( s minus last character)


void writeBackward2 (string s):
   if s is empty
     do nothing
   if s isn't empty
     writeBackward2 (s minus first character)
     output first character of s


So I'm wondering if there is some fast way to look at any recursion
pattern and figure out ways to rewrite it in other forms quickly. For
instance, in this case, in the first version you traverse the string
from right to left, and do the thing (output, in this case), before the
recursion; the next version has both the direction of the string
traversal switched and the order of the recursion call. Can simple
patterns like this be abstracted into universal laws?

Reply via email to