Exercise 6 for functional scala. (p.26)

(this one was fun...)