正如我在讀編程F#的書,我發現195頁的示例代碼片段如下:使用延續到二進制遞歸轉換爲尾遞歸
type ContinuationStep<'a> =
| Finished
| Step of 'a * (unit -> ContinuationStep<'a>)
let iter f binTree =
let rec linearize binTree cont =
match binTree with
| Empty -> cont()
| Node(x, l, r) ->
Step(x, (fun() -> linearize l (fun() -> linearize r cont)))
let steps = linearize binTree (fun() -> Finished)
let rec processSteps step =
match step with
| Finished ->()
| Step(x, getNext)
-> f x
processSteps (getNext())
processSteps steps
通過使用延續,穿越的二進制遞歸二進制已被轉換爲尾遞歸函數processSteps
。我的問題是,另一個函數linearize
似乎是非尾遞歸的。這是否意味着即使使用延續,我們也不能將二元遞歸轉換爲尾遞歸?