Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

On the basic concepts of Loop & iteration & backtracking & Recursion & Recursion

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Loop: to perform an operation or operation over and over again.

Iteration: continue to operate on the previous old value to get a new value until the precision is reached. It is generally used to get the approximate target value, loop the same expression (function) repeatedly, and always invert the result of the previous operation for the next operation.

Recursion: repeat an operation from the initial value to get the desired result. -from the known to the unknown, from childhood to childhood (for example, the annual growth of 9cm, 180,270 after 30 years)

Backtracking: a process experienced during recursion.

Recursion: keep going back to the previous operation from the desired result until you go back to the initial value and then recursively get the desired result-from unknown to known, from big to small, and then from small to big (if you want to enter the bat, then the programming is great, you have to unload the player's pesticides and study hard). Recursion is derived from induction (Induction).

An operation (operation) can call its own operation form constantly, and often need to get the result of the current operation through the previous result. Therefore, when the program is running, always "backtrack" the previous result again and again (in the process of backtracking, these results are unknown until the backtracking is terminated by the initial value, and then recursively to get the currently required value).

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

Explicit recursive termination method (a recursion must be defined by his recursion to the head, otherwise it will be infinitely recursive)

Explicit termination handling method

Call yourself repeatedly and reduce the size of the problem

Endless loops will not overflow the stack, but infinite recursion will cause the overflow of the stack. For more information, it is recommended to read: "Recursion-the Beauty of the Program, and the difference from the Loop," but in fact, business models rarely encounter it.

Kidneyball Zhihu's answer to the summary meeting is incisive.

In languages with loops, some people think that tail recursive optimization is completely useless except for showing off. In fact, in my opinion, tail-recursive writing has the following advantages

Forces you to write the loop as a separate function. What's the advantage of that? This will affect your programming style, and after you are used to using tail recursion, you are much less likely to write a large function with hundreds of lines.

Ensure that there are no side effects and uniformly use immutable data. In a loop, a loop variable is a variable data. As a human flesh developer, if you want to make sure that your program is free of side effects, the best thing to do is not to write anything with side effects at all, so that you can see if there are any side effects at a glance during code review. As for the benefits of avoiding side effects, you can write another article, which will not be expanded here.

It looks better when converted to an inert sequence. In some languages, the tail recursive form 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, which is characterized by: I do not pay attention to the problem itself, I only focus on how the problem can be decomposed into smaller sub-problems in a repeatable way, and the relationship between these sub-problems and the original problem. In addition, when the scale of the problem is small enough, there is a simple and direct solution.

Recursion and Recursion are still confused, show code

/ / Recursively solve function fib (n) {return n

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report