我有我相信是一個相當簡單的尾遞歸函數。然而,@tailrec
告訴我,否則。尾遞歸失敗(可能是因爲隱式缺點轉換)
@tailrec
def _next(continue : String, current : List[Long], first : Boolean) : Stream[Long] = {
current match {
case head :: tail => head #:: _next(continue, tail, false) //this line breaks tailrec
case Nil if first => empty
case _ => {
val (nc, nl) = getIds(continue)
_next(nc, nl, true)
}
}
}
呈現我
[error] could not optimize @tailrec annotated method _next: it contains a recursive call not in tail position
[error] case head :: tail => head #:: _next(continue, tail, false)
[error] ^
它可能與我從日食接受隱通知做的事:- Implicit conversions found: _next(continue, tail, false) => consWrapper(_next(continue, tail, false))
,但不幸的是,這是沒有幫助我解決問題。
我該如何解決這個問題,並且,對於布朗尼分,我哪裏去錯了,認爲這會尾部遞歸?
你最後一次操作是** **附加,而不是'_next'電話 - 使用遞歸的蓄電池 –