正如我在读编程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
似乎是非尾递归的。这是否意味着即使使用延续,我们也不能将二元递归转换为尾递归?