object FoldRight { def foldRight[A, B](as: List[A], z: B)(f: (A, B) => B): B = as match { case Nil => z case ::(x, xs) => f(x, foldRight(xs, z)(f)) } def main(args: Array[String]): Unit = { println(foldRight(List(1, 2, 3), Nil: List[Int])(::(_, _))) } }
List(1, 2, 3)
时间: 2024-10-12 06:40:42