Can every recursion be converted into iteration?

前端 未结 17 2285
遥遥无期
遥遥无期 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.

提交回复
热议问题