我正在使用Scala进行学习 我编写了这段代码来对列表中的元素进行排序
def isort(xs:List[Int]):List[Int]=
xs match{
case List() => xs
case y::ys => insert(y,isort(ys))
}
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)
}
但是我收到以下错误:
Constructor can not be instantiated to expected type found Scala.collection.Immutable required List[Int]
in
`y::ys => insert(y,isort(ys))`
和我使用::
的类似错误答案 0 :(得分:1)
尝试使用REPL的粘贴模式。这将允许您在相同的上下文中定义两个def:
scala> :paste
// Entering paste mode (ctrl-D to finish)
def isort(xs:List[Int]):List[Int]=
xs match{
case List() => xs
case y::ys => insert(y,isort(ys))
}
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)
}
// Exiting paste mode, now interpreting.
isort: (xs: List[Int])List[Int]
insert: (x: Int, xs: List[Int])List[Int]