Solution to problem 22 using closures