0

I am using Scala for studying purpose I wrote this code to sort the elements in list

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)

  }

but I am getting the following error:

Constructor can not be instantiated to expected type found Scala.collection.Immutable required List[Int]
 in  
 `y::ys => insert(y,isort(ys))`

and similar error where I use ::

I refer tutorial : https://class.coursera.org/progfun-005/lecture

1
  • i got my error .I define a similar class name in my package and it taking that class as a reference and so it does not know about method :: Commented Jul 16, 2015 at 11:24

1 Answer 1

1

Try using the paste mode of the REPL. This will allow you to define the two defs in the same context:

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]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.