implement exercise 13 from "FP in Scala" book, chapter 5