I wrote a function like this in Scala:
def isSorted[T](list : List[T])(compare : (T, T) => Boolean) : Boolean = {
list match {
case Nil => true
case x :: Nil => true
case x :: rest => !compare(rest.head, x) && isSorted(rest)(compare)
}
}
I am curious whether the compiler will optimize away the recursive call. The recursive call can only happen if the leading comparison succeeds. If not, is there a way to bomb out early and still achieve tail recursion optimization?