RECURSION

- a factorial example


Recursion is one of the most fundamental and beautiful ideas in computer science. A function is said to be "recursive" if it calls itself. Recursive control structure is the main topic of this and the following lecture.

The best way to learn recursion is by an example. Suppose you'd like to find out the length of a given a list L. This can be achieved by adding 1 to the length of cdr(L), given the length of () equals 0:


Following example illustrates how to calculate the factorial of number n. Since fact(0) = 1 and fact(n) = n*fact(n-1):

The interpreter remembers all functions and variables so far:

Click here to see how to set up an Input Applet.