以下のコード (非常に些細な最大値とリストの合計) では、メソッドの最後で呼び出される再帰関数があります。scala コンパイラはこれを末尾再帰として扱い、スタック フレームの使用を最適化しますか? これを確認するにはどうすればよいですか?
package example
import common._
object Lists {
def sum(xs: List[Int]): Int = {
def recSum(current: Int, remaining: List[Int]): Int = {
if (remaining.isEmpty) current else recSum(current + remaining.head, remaining.drop(1))
}
recSum(0, xs)
}
def max(xs: List[Int]): Int = {
def recMax(current: Int, remaining: List[Int], firstIteration: Boolean): Int = {
if(remaining.isEmpty){
current
}else{
val newMax = if (firstIteration || remaining.head>current) remaining.head else current
recMax(newMax, remaining.drop(1), false)
}
}
if (xs.isEmpty) throw new NoSuchElementException else recMax(0, xs, true)
}
}