# Recursion and iteration

Source: Internet
Author: User

In my education, recursion seems a mysterious thing. It is hard to say clearly, it is a senior product, like a fog. In the course of my high school study, I didn't understand what recursion was at first. The teacher may think that we are hard to understand, so I didn't talk about it in a unified way later. And I also think it seems very difficult, and it does not seem to need to be used, so there is no such thing. Later, when I arrived at the university, the teacher who taught programming languages ignored recursion. At that time, I knew the importance of recursion. Then, she turned out to be confused when she said something untenable, many of them are syntactic sugar.

It wasn't until I saw the tool.

First, it is explained that the expansion of the model is based on the scheme language, a dialect of lisp. It does not have a circular statement. Therefore, in terms of syntax form, all repetitive operations are recursive. So in general thinking, is there no iteration? Because many imperative languages such as C and java have cyclic statements, we generally write iteration statements instead of recursive functions, this is a bit confusing. In scheme, this classification based on specific computing processes is divided into Recursive Computing and Iterative Computing processes. Most of the time, we ignore the computing mode based on the syntax format, which is actually not accurate.

(The following is a quote from the Chinese version of sicp:

When we talk about a process as recursion, we discuss a fact in the syntax form, indicating that the process itself is referenced in the definition of this process (either directly or indirectly. When a computing process has a certain pattern (for example, linear recursion), we are talking about the progress of this computing process, rather than the syntax format written in the corresponding process .)

In general, recursive computation presents a computing mode that expands first and then shrinks, that is, the interpreter (or compiler) needs to maintain some information and will increase with the extension. The Iterative Computing process does not expand or contract. It only requires a few fixed necessary information. Using the factorial function can be very simple:

; Recursive calculation process

`(Define (factorial n) (if (= n 1) 1 (* (factorial (-n 1) n ))); iteration calculation process (define (fact-iter counter result) (if (= counter 1) result (fact-iter (-counter 1) (* counter result )))) (define (factorial1 n) (fact-iter n 1); recursive calculation process (define (factorial n) (if (= n 1) 1 (* (factorial (-n 1); define (fact-iter counter result) (if (= counter 1) result (fact-iter (-counter 1) (* counter result) (define (factorial1 n) (fact-iter n 1 ))`

Of course, recursive computation can also be simply divided into linear recursive computation (factorial) and tree-like recursive computation (Fibonacci ). Obviously, tree recursion involves a lot of redundant computing, But it features conciseness.

For the time being, let's talk about so much. The next article will give several examples to illustrate recursive and Iterative Computing processes.

Related Keywords:
Related Article

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.