Exercise 3.6, non-tail recursive version