繁体   English   中英

每个迭代的 function 可以转换成递归的 function 吗?

[英]Can every iterative function be converted into a recursive function?

现在一些编译器可以帮助我们将递归函数转换为迭代,但我的问题是每个迭代 function 可以有一个递归对应吗?

每个迭代的 function 都可以转换为递归,反之亦然。 Church-Turing 假设说,图灵机是一个通用计算系统:任何可计算的 function 都可以实现为图灵机。 如果您的语言可以实现图灵机,则它是图灵等效的,因此可以计算任何可计算的 function。

我们可以使用没有迭代的递归来实现图灵机吗? 我们当然可以

TuringMachine(tape[1...n...], head, state)
    if state = halt_accept then return true
    if state = halt_reject then return false
    (t, h, s) = RunTransition(tape[head], state)
    tape[head] = t
    return TuringMachie(tape, h, s)

The function RunTransition just checks the transition table for a matching row for the current tape symbol and state, and returns the new tape symbol, tape head position and state. 这应该说明,原则上,递归完全足以实现图灵机,这意味着任何可计算的 function 都可以通过递归来解决(如果您相信 Church-Turing 假设)。 因为迭代函数只能做图灵机(同样,如果你接受 Church-Turing 假设),那么任何迭代过程都可以变成递归过程。

For 循环不难想象:

function Foo()
    for i = 1 to n
        f(i)
Foo()

... becomes ...

function Foo(i)
    f(i)
    if i + 1 < n then foo(i + 1)

Foo(0)

While 循环也进行了类似的转换:

function Foo()
    condition = true
    while condition
        f()

Foo()

... becomes ...

Foo()
    if condition then
        f()
        Foo()

Foo()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM