implement exercises 16-24 from "FP in Scala" book, chapter 3