Next: , Previous: , Up: Top   [Contents][Index]

11 Loops and Recursion

Emacs Lisp has two primary ways to cause an expression, or a series of expressions, to be evaluated repeatedly: one uses a while loop, and the other uses recursion.

Repetition can be very valuable. For example, to move forward four sentences, you need only write a program that will move forward one sentence and then repeat the process four times. Since a computer does not get bored or tired, such repetitive action does not have the deleterious effects that excessive or the wrong kinds of repetition can have on humans.

People mostly write Emacs Lisp functions using while loops and their kin; but you can use recursion, which provides a very powerful way to think about and then to solve problems12.



You can write recursive functions to be frugal or wasteful of mental or computer resources; as it happens, methods that people find easy—that are frugal of mental resources—sometimes use considerable computer resources. Emacs was designed to run on machines that we now consider limited and its default settings are conservative. You may want to increase the values of max-specpdl-size and max-lisp-eval-depth. In my .emacs file, I set them to 15 and 30 times their default value.