Exercise 5.1, tail recursive version