Exercise 3.27, naive implementation