【发布时间】:2015-08-25 19:45:30
【问题描述】:
所以我对 Scala 比较陌生,并且在 Scala 中进行函数式编程,并且在其中一个实践问题中遇到了一些新问题。在 hasSequences 中,我需要将 return 关键字放在我的返回值前面,否则什么都不会返回,下一行将执行。为什么需要这个?
def hasSubsequence[A](list: List[A], subList: List[A]) : Boolean = {
def matcher(l: List[A], sl: List[A]): List[A] = {
if (sl.isEmpty) return Nil
l match {
case Nil => Nil
case head :: tail if (head == sl.head) => head :: matcher(tail, sl.tail)
case head :: tail => matcher(tail, sl)
case _ => Nil
}
}
if (matcher(list, subList) == subList) true else false
}
【问题讨论】:
标签: scala functional-programming return pattern-matching