List ZipWith

object ZipWith {

  def zipWith[T](l1: List[T], l2: List[T])(f: (T, T) => T): List[T] = (l1, l2) match {
    case (_, Nil)             => Nil
    case (Nil, _)             => Nil
    case (h1 :: t1, h2 :: t2) => f(h1, h2) :: zipWith(t1, t2)(f)
  }

  def main(args: Array[String]): Unit = {
    println(zipWith(List(1, 2, 3), List(4, 5, 6, 7))(_ + _))
  }

}
List(5, 7, 9)

 

posted on 2016-04-23 08:27  JonkeyGuan  阅读(190)  评论(0编辑  收藏  举报