[This page is auto-generated; please prefer to read the article’s PDF Form.]
Consider the 4-tuple (n,s,d,a) formed by the arguments of recur(), which will be referred as “args-tuple”. Notice that the args-tuple for its two recursive (“child”) calls, (n − 1,s,a,d) and (n − 1,a,d,s), are simple transformations of the args-tuple (n,s,d,a) of the current (“parent”) call. Specifically, the transformation would consist of decrementing n and swapping (d,a) or (s,a) (for the first and second recursive calls respectively). Thus, args-tuple for the child calls are easily obtainable from that of the parent’s call.
Interestingly, these transformations are reversible and hence allow obtaining back the parent’s args-tuple from the corresponding child’s args-tuple.
This suggests that we can maintain a single global 4-tuple of arguments, g, which would correspond to the arguments of the current recur() call and will be modified by appropriate transformations (as mentioned above) during recursive calls and their return. Then, method recur() would access its arguments from this global 4-tuple g and so can be made parameter-less.
Following equivalent of recur() is based on the above idea:
Copyright © 2021 Nitin Verma. All rights reserved.