Loop: to perform an operation over and over again.

Iteration: Continuous operation of an old value to obtain a new value until accuracy is achieved. Generally used to get an approximate target value, repeating the same operation (function), and always inverting the result of the previous operation for the next operation

Recursion: An operation performed repeatedly from an initial value to a desired result. —– From known to unknown, from small to small (e.g. 9cm per year, 180 in 20 years, 270 after 30 years)

Backtracking: A process that occurs when recursing.

Recursion: from the required results continue to go back to the previous operation until back to the initial value of recursion to get the required results —- from unknown to known, from large to small, and then from small to large (you want to enter bat, then programming on the cow force, have to uninstall the player pesticide, hard to learn). Recursion is derived from Induction.

An operation (operation), can through continuous operation form of the call itself, often need to get the current through the result of the previous operation results, therefore, when the program runs, always first to “go back” again and again the result of the previous (back in the process of these results are unknown, until back to the initial value to back in the end, layers of recursive back to get the requirements of the current value)

A complete recursion should have the following three conditions, otherwise it is unqualified recursion

  1. Specify the terminating method of recursion (a recursion must be defined as the end of the recursion, otherwise it will be infinite recursion)

  2. Explicit termination handling

  3. Call yourself repeatedly and reduce the size of the problem

An infinite loop does not overflow the stack and an infinite recursion does. For more details, read recursion: The Beauty of Programs and How They Differ from Loops. But in practice, business models rarely encounter them.

The conclusion of kidneyBall’s answer session is excellent

In looping languages, some people think tail-recursive optimization is completely useless except for showmanship. Well, no, tail recursion has the following benefits in my opinion

  1. It forces you to write the loop as a separate function. What good is that? This will affect your programming style, and when you get used to tail recursion, you’re much less likely to write a function that’s hundreds of lines long.

  2. Guarantee no side effects, unified use of immutable data. In a loop, a loop variable is a variable data. As a human developer, if you want to ensure that a piece of your program has no side effects, the best thing you can do is not write anything with side effects at all, so that the code review will see if there are any side effects. As to the benefits of avoiding side effects, this could write another article, but will not be expanded here.

  3. It looks better when converted to an inert sequence. In some languages, tail recursion is basically an inert sequence that can be used directly by removing the loop variable (becoming infinite recursion) and taking the initial state as the first element.

Recursion is a way of thinking in which I don’t focus on the problem, I focus only on how the problem can be broken down into smaller subproblems in a repeatable way, and how those subproblems relate to the original problem. Plus, when the size of the problem is small enough, there is a simple and straightforward solution.


Recursion and recursion are confused, show code

// Solve recursivelyfunction fib(n){
    returnn <2? 1:fib(n-1) + fib(n-2); } // Solve the problem recursivelyfunction fib(n){
    let start=0;
    let fn=1;
    for (leti=0; i<n; i++) {let t=fn;
        fn=fn+start;
        start=t;
    }
    return fn;
}Copy the code

As you can see,

The general way of writing the program is like the general formula for the sequence. The recursion of the program is like the recursion formula of the sequence.

Talk about the cycle & iteration & backdate & recursion & recursion these basic concepts – model design, domain design, software design – zhou Army’s personal website, not regularly updated, the article is inappropriate, please leave a message to inform, thank you (talk about the series more for the summary article (move brick)).

Recommended articles:

Programmers, never ask me about recursion again

Recursion – The beauty of programs and how they differ from loops