implement exercises 4-15 from "FP in Scala" book, chapter 3