CIS-3030 Homework #4: Higher Order Workhorses

Due: Wednesday, March 13, 2019

Read Chapter 8 in PiS.

  1. There are four "workhorse" higher order methods in the various Scala collection classes: foreach, filter, map, and flatMap. Although these methods exist already in the Scala library, for this assignment you are to implement them as functions that can be applied to generic lists (that is, lists of any element type). This will give you more practice with recursion. Use list pattern matching. Do not use any vars or while loops. Write at least two of the methods tail recursively using the @tailrec annotation to have the compiler check your work.

            object Homework04 {
              def filter[A](list: List[A], pred: A => Boolean): List[A] = ???
              def foreach[A](list: List[A], f: A => Unit): Unit = ???
              def map[A, B](list: List[A], trans: A => B): List[B] = ???
              def flatMap[A, B](list: List[A], trans: A => List[B]): List[B] = ???
  2. Include in your file some test/demonstration code that exercises your methods to verify that they work.

Submit your file Hw04.scala to Moodle.

Last Revised: 2019-03-06
© Copyright 2019 by Peter C. Chapin <>