Can every recursion be converted into iteration?

前端 未结 17 2308
遥遥无期
遥遥无期 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:09

    Have a look at the following entries on wikipedia, you can use them as a starting point to find a complete answer to your question.

    • Recursion in computer science
    • Recurrence relation

    Follows a paragraph that may give you some hint on where to start:

    Solving a recurrence relation means obtaining a closed-form solution: a non-recursive function of n.

    Also have a look at the last paragraph of this entry.

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

    Can you always turn a recursive function into an iterative one? Yes, absolutely, and the Church-Turing thesis proves it if memory serves. In lay terms, it states that what is computable by recursive functions is computable by an iterative model (such as the Turing machine) and vice versa. The thesis does not tell you precisely how to do the conversion, but it does say that it's definitely possible.

    In many cases, converting a recursive function is easy. Knuth offers several techniques in "The Art of Computer Programming". And often, a thing computed recursively can be computed by a completely different approach in less time and space. The classic example of this is Fibonacci numbers or sequences thereof. You've surely met this problem in your degree plan.

    On the flip side of this coin, we can certainly imagine a programming system so advanced as to treat a recursive definition of a formula as an invitation to memoize prior results, thus offering the speed benefit without the hassle of telling the computer exactly which steps to follow in the computation of a formula with a recursive definition. Dijkstra almost certainly did imagine such a system. He spent a long time trying to separate the implementation from the semantics of a programming language. Then again, his non-deterministic and multiprocessing programming languages are in a league above the practicing professional programmer.

    In the final analysis, many functions are just plain easier to understand, read, and write in recursive form. Unless there's a compelling reason, you probably shouldn't (manually) convert these functions to an explicitly iterative algorithm. Your computer will handle that job correctly.

    I can see one compelling reason. Suppose you've a prototype system in a super-high level language like [donning asbestos underwear] Scheme, Lisp, Haskell, OCaml, Perl, or Pascal. Suppose conditions are such that you need an implementation in C or Java. (Perhaps it's politics.) Then you could certainly have some functions written recursively but which, translated literally, would explode your runtime system. For example, infinite tail recursion is possible in Scheme, but the same idiom causes a problem for existing C environments. Another example is the use of lexically nested functions and static scope, which Pascal supports but C doesn't.

    In these circumstances, you might try to overcome political resistance to the original language. You might find yourself reimplementing Lisp badly, as in Greenspun's (tongue-in-cheek) tenth law. Or you might just find a completely different approach to solution. But in any event, there is surely a way.

    0 讨论(0)
  • 2020-11-22 02:10
    • Recursive function execution flow can be represented as a tree.

    • The same logic can be done by a loop, which uses a data-structure to traverse that tree.

    • Depth-first traversal can be done using a stack, breadth-first traversal can be done using a queue.

    So, the answer is: yes. Why: https://stackoverflow.com/a/531721/2128327.

    Can any recursion be done in a single loop? Yes, because

    a Turing machine does everything it does by executing a single loop:

    1. fetch an instruction,
    2. evaluate it,
    3. goto 1.
    0 讨论(0)
  • 2020-11-22 02:12

    Basically yes, in essence what you end up having to do is replace method calls (which implicitly push state onto the stack) into explicit stack pushes to remember where the 'previous call' had gotten up to, and then execute the 'called method' instead.

    I'd imagine that the combination of a loop, a stack and a state-machine could be used for all scenarios by basically simulating the method calls. Whether or not this is going to be 'better' (either faster, or more efficient in some sense) is not really possible to say in general.

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

    It is possible to convert any recursive algorithm to a non-recursive one, but often the logic is much more complex and doing so requires the use of a stack. In fact, recursion itself uses a stack: the function stack.

    More Details: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions

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

    I'd say yes - a function call is nothing but a goto and a stack operation (roughly speaking). All you need to do is imitate the stack that's built while invoking functions and do something similar as a goto (you may imitate gotos with languages that don't explicitly have this keyword too).

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