Example 3.20, naive version
Example 3.20, tail recursive version