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)
时间: 2024-10-05 14:43:13