scala 中的临时函数,用于递归连接两个列表: 不使用"concat"



我想在Scala中有一个函数可以连接两个列表,而不使用内置函数。它应该以函数递归的方式编写,但我不确定从哪里开始。

def concatList(lleft: List[String], lright: List[String]): List[String] {
}

希望你现在已经解决了你的作业问题。如果将来有人碰到这个问题,你可以像这样使用尾部递归

def concatList(lleft: List[String], lright: List[String]): List[String] = lright match {
case Nil => lleft  // base condition
case head :: tail =>  // add first element to end of lleft and recurse
concatList(lleft :+ head, tail)
}

最新更新