Recursion (computer science)

Tree created using the Logo programming language and relying heavily on recursion. Each branch can be seen as a smaller version of a tree.
Recursive drawing of a Sierpiński Triangle through turtle graphics

In computer science, recursion is a method of solving a computational problem where the solution depends on solutions to smaller instances of the same problem.[1][2] Recursion solves such recursive problems by using functions that call themselves from within their own code. The approach can be applied to many types of problems, and recursion is one of the central ideas of computer science.[3]

The power of recursion evidently lies in the possibility of defining an infinite set of objects by a finite statement. In the same manner, an infinite number of computations can be described by a finite recursive program, even if this program contains no explicit repetitions.

— Niklaus Wirth, Algorithms + Data Structures = Programs, 1976[4]

Most computer programming languages support recursion by allowing a function to call itself from within its own code. Some functional programming languages (for instance, Clojure)[5] do not define any looping constructs but rely solely on recursion to repeatedly call code. It is proved in computability theory that these recursive-only languages are Turing complete; this means that they are as powerful (they can be used to solve the same problems) as imperative languages based on control structures such as while and for.

Repeatedly calling a function from within itself may cause the call stack to have a size equal to the sum of the input sizes of all involved calls. It follows that, for problems that can be solved easily by iteration, recursion is generally less efficient, and, for certain problems, algorithmic or compiler-optimization techniques such as tail call optimization may improve computational performance over a naive recursive implementation.

  1. ^ Graham, Ronald; Knuth, Donald; Patashnik, Oren (1990). "1: Recurrent Problems". Concrete Mathematics. Addison-Wesley. ISBN 0-201-55802-5.
  2. ^ Kuhail, M. A.; Negreiros, J.; Seffah, A. (2021). "Teaching Recursive Thinking using Unplugged Activities" (PDF). World Transactions on Engineering and Technology Education. 19: 169–175.
  3. ^ Epp, Susanna (1995). Discrete Mathematics with Applications (2nd ed.). PWS Publishing Company. p. 427. ISBN 978-0-53494446-9.
  4. ^ Wirth, Niklaus (1976). Algorithms + Data Structures = Programs. Prentice-Hall. p. 126. ISBN 978-0-13022418-7.
  5. ^ "Functional Programming | Clojure for the Brave and True". www.braveclojure.com. Retrieved 2020-10-21.

Recursion (computer science)

Dodaje.pl - Ogłoszenia lokalne