refactored problem 9 to use pattern matching