I am writing a parameterized merge sort functionality and passing the less checker as a function. However, the compiler is throwing following error.
type mismatch;
found : y.type (with underlying type T)
required: T
Here is my full code
def mergeSort[T] (list:List[T], pred:(T,T) =>Boolean):List[T]={
def merge[T](left:List[T], right:List[T], acc:List[T]):List[T] = (left,right) match{
case (Nil,_) => acc ++ right
case (_,Nil) => acc ++ left
case (x::xs, y::ys) => if(pred(y,x)) merge(left,ys,acc :+ y) else merge(xs,right,acc :+ x)
}
val m = list.length/2
if (m == 0) list
else {
val (l,r) = list splitAt m
merge(mergeSort(l,pred), mergeSort(r,pred), List())
}
}
The problem is at line
if(pred(y,x))
Everything seems logically correct, can't figure out why this is happening? help appreciated.
merge[T](withmerge(.