
Design Using Recursion 185
Comparing these two implementations we get an idea of how to develop a recursive algorithm from an
iterative one. One way is to compare the execution traces (ET, see Section 8.2):
iter(): i ← n, f (i) → True, Block, i ← g(i), f (i) → True, Block, ..., i ← g(i), f (i) → False, exit
rec(): n, f (n) → True, Block, n ← g(n), rec(n) ↓
n1, f (n1) → True, Block, ... ↓
... , np ← g(np), rec(np)↓
np′, f (np′) → False, exit
Note that as n is not used further in the rec() after n = g(n), we can write a single statement rec( g(n))
in place of the two statements n = g(n); rec(n); and the algorithm will look more compact. Also, ...