我编写了以下基于模式匹配的函数:
def replacingElem(lines: List[String], last10: String): List[String] =
{
if (lines.isEmpty) Nil else {
val row = lines.head.split("[ \t]+")
row(0).toInt match {
case 10 => lines.head :: replacingElem(lines.tail, row(1))
case 15 => (row(0) + " " + last10 + " " + row(2) + " " + row(3)) :: replacingElem(lines.tail, last10)
case _ => lines.head :: replacingElem(lines.tail, last10)
}
}
}
我的目标是使整数 10 和 15 可调,并将它们作为函数的参数。我做了以下修改:
def replaceIndex(lines: List[String], last10: String,a:Int,b:Int): List[String] = {
if (lines.isEmpty) Nil else {
val row = lines.head.split("[ \t]+")
row(0).toInt match {
case a => lines.head :: replaceIndex(lines.tail, row(1),a,b)
case b => (row(0) + " " + last10 + " " + row(2) + " " + row(3)) :: replaceIndex(lines.tail, last10,a,b)
case _ => lines.head :: replaceIndex(lines.tail, last10,a,b)
}
}
}
但我得到了编译错误。