Exercise 3.22, naive version
Example 3.22, tail recursive version