Can every recursion be converted into iteration?

前端 未结 17 2310
遥遥无期
遥遥无期 2020-11-22 01:59

A reddit thread brought up an apparently interesting question:

Tail recursive functions can trivially be converted into iterative functions. Other one

相关标签:
17条回答
  • 2020-11-22 02:27

    Is it always possible to write a non-recursive form for every recursive function?

    Yes. A simple formal proof is to show that both µ recursion and a non-recursive calculus such as GOTO are both Turing complete. Since all Turing complete calculi are strictly equivalent in their expressive power, all recursive functions can be implemented by the non-recursive Turing-complete calculus.

    Unfortunately, I’m unable to find a good, formal definition of GOTO online so here’s one:

    A GOTO program is a sequence of commands P executed on a register machine such that P is one of the following:

    • HALT, which halts execution
    • r = r + 1 where r is any register
    • r = r – 1 where r is any register
    • GOTO x where x is a label
    • IF r ≠ 0 GOTO x where r is any register and x is a label
    • A label, followed by any of the above commands.

    However, the conversions between recursive and non-recursive functions isn’t always trivial (except by mindless manual re-implementation of the call stack).

    For further information see this answer.

    0 讨论(0)
  • 2020-11-22 02:27

    All computable functions can be computed by Turing Machines and hence the recursive systems and Turing machines (iterative systems) are equivalent.

    0 讨论(0)
  • 2020-11-22 02:29

    Appart from the explicit stack, another pattern for converting recursion into iteration is with the use of a trampoline.

    Here, the functions either return the final result, or a closure of the function call that it would otherwise have performed. Then, the initiating (trampolining) function keep invoking the closures returned until the final result is reached.

    This approach works for mutually recursive functions, but I'm afraid it only works for tail-calls.

    http://en.wikipedia.org/wiki/Trampoline_(computers)

    0 讨论(0)
  • 2020-11-22 02:30

    Sometimes replacing recursion is much easier than that. Recursion used to be the fashionable thing taught in CS in the 1990's, and so a lot of average developers from that time figured if you solved something with recursion, it was a better solution. So they would use recursion instead of looping backwards to reverse order, or silly things like that. So sometimes removing recursion is a simple "duh, that was obvious" type of exercise.

    This is less of a problem now, as the fashion has shifted towards other technologies.

    0 讨论(0)
  • 2020-11-22 02:34

    tazzego, recursion means that a function will call itself whether you like it or not. When people are talking about whether or not things can be done without recursion, they mean this and you cannot say "no, that is not true, because I do not agree with the definition of recursion" as a valid statement.

    With that in mind, just about everything else you say is nonsense. The only other thing that you say that is not nonsense is the idea that you cannot imagine programming without a callstack. That is something that had been done for decades until using a callstack became popular. Old versions of FORTRAN lacked a callstack and they worked just fine.

    By the way, there exist Turing-complete languages that only implement recursion (e.g. SML) as a means of looping. There also exist Turing-complete languages that only implement iteration as a means of looping (e.g. FORTRAN IV). The Church-Turing thesis proves that anything possible in a recursion-only languages can be done in a non-recursive language and vica-versa by the fact that they both have the property of turing-completeness.

    0 讨论(0)
提交回复
热议问题