我从Scala开始,遵循Coursera-Scala课程中的函数式编程原理以及演示文稿中的粘贴代码,我遇到了两个我不理解的错误:
line 2: Expression of type Nil[Nothing] doesn't conform to expected type List [Int]
line3: Pattern type is incompatible with expected type, found: ::[B], required: List[Int]
我假设代码是正确的,因为它是Martin Odersky ppt的复制粘贴。
def isort (xs: List[Int]): List[Int] = xs match {
case List() => List()
case y :: ys => insert(y, isort(ys))
}
如果有人能解释为什么我会得到错误,将不胜感激。
答案 0 :(得分:0)
无需手动导入List
-您不需要这样做。
以下内容在REPL会议中对我有用。
def insert (x: Int, xs: List[Int]): List[Int] = xs match {
case List() => List (x)
case y :: ys => if (x <= y) x::xs else y::insert(x,ys)
}
def isort (xs: List[Int]): List[Int] = xs match {
case List() => List()
case y :: ys => insert(y, isort(ys))
}