列表对列表添加的生成函数。
我不明白为什么它不工作
def lconcat(l: List[List[Int]]): List[Int] = {
return l.foldRight(1)((x:List[Int], y:List[Int]) => x ++ y)
}
和我调用像
这样的函数println(lconcat(List(List(1, 2, 3), List(4, 5, 6))))
我想要的结果像List[Int] = List(1, 2, 3, 4, 5, 6)
由于您想要返回List of Integers,所以foldRight
的参数应该是一个空列表,输入的列表将被连接到它上面。
你的代码应该是:
def lconcat(l: List[List[Int]]): List[Int] = {
l.foldRight(List.empty[Int])((x:List[Int], y: List[Int]) => {
x ++ y
})
}