我对 Scala 很陌生,想知道这是否是正确的写作方式:
def createCol(prior: List[Int], current: List[Int]): List[Int] = {
if (prior.isEmpty) List(1) ++ current
else if (prior.tail.isEmpty) // begin of the block to improve
createCol(prior.tail, current ++ List(prior.head))
else
createCol(prior.tail, current ++ List(prior.head + prior.tail.head))
}
我感兴趣的部分是:
if (prior.tail.isEmpty)
createCol(prior.tail, current ++ List(prior.head))
else
createCol(prior.tail, current ++ List(prior.head + prior.tail.head))
因为我重复几乎相同的函数调用createCol
,所以我尝试了这个:
val ssum = if (prior.tail.isEmpty) prior.head else prior.head + prior.tail.head
createCol(prior.tail, current ++ List(ssum))
最好或推荐的方法是什么?
谢谢