Example 4.4, tail recursive version