Exercise 6 for functional scala.

(this one was fun...)