使用 foldLeft 函数在 Scala 中使用函数来反转列表:
def reverseWithFold(ls: List[Int]): List[Int] =
ls.foldLeft(List[Int]())((c, _) => _:::c)
它出现编译错误:Expression of type List[B_] => List[Int] doesn’t comforrm to expected type List[Int](PS:我知道我的解决方案不正确)
我能知道这是什么意思吗?在_:::c这里, the_代表cin (c, _), andc是一个List[Int]类型,所以在我看来,_:::c应该是两个 的串联List[Int]。它应该符合预期的类型,对吧?