Exercise 5.2, tail recursive versions