我有一个构建树的简单方法:
@tailrec final def buildTree (X:DenseMatrix[Double], Y:DenseVector[Double], minBucket:Int):Node = {
// Get the split variable, split point and data
val (splitVar, splitPoint, leftX, leftY, rightX, rightY) = chooseSplit(X, Y, minBucket);
// If we couldn't find a split, then we have a leaf
if(splitVar == Double.NegativeInfinity){
new Node(Y)
}else{
// Otherwise recursively build the children and create yourself as a vertex
val left = buildTree(leftX, leftY, minBucket)
val right = buildTree(rightX, rightY, minBucket)
new Node(Y, splitVar, splitPoint, left, right)
}
然而,当我去编译时,我得到:
could not optimize @tailrec annotated method buildTree: it contains a recursive call not in tail position
我在OCaml中完成了这样的事情,没有任何问题。有解决办法吗?
答案 0 :(得分:1)
我认为错误消息非常明确:您的方法既不是private
也不是final
,因此可以覆盖它,因此无法保证对buildTree
的调用将会进行以相同的方法。该方法需要是private
或final
。
但即使 private
或final
,递归调用也不在尾部位置。
基本上,你的尾递归调用既不是尾调用,也不保证是递归调用。