triangle-recursively is called with an argument of
ifexpression is evaluated first. This is the do-again test and returns false, so the else-part of the
ifexpression is evaluated. (Note that in this example, the do-again-test causes the function to call itself when it tests false, not when it tests true.)
We already know what happens when Emacs evaluates
an argument of 2. After going through the sequence of actions described
earlier, it returns a value of 3. So that is what will happen here.
The value returned by the function as a whole will be 6.
Now that we know what will happen when
called with an argument of 3, it is evident what will happen if it is
called with an argument of 4:
In the recursive call, the evaluation of(triangle-recursively (1- 4))
will return the value of evaluating(triangle-recursively 3)
which is 6 and this value will be added to 4 by the addition in the third line.
The value returned by the function as a whole will be 10.
triangle-recursively is evaluated, it evaluates a
version of itself—a different instance of itself—with a smaller
argument, until the argument is small enough so that it does not
Note that this particular design for a recursive function requires that operations be deferred.
(triangle-recursively 7) can calculate its answer, it
(triangle-recursively 6); and before
(triangle-recursively 6) can calculate its answer, it must call
(triangle-recursively 5); and so on. That is to say, the
(triangle-recursively 7) makes must be
(triangle-recursively 6) makes its calculation;
(triangle-recursively 6) must defer until
(triangle-recursively 5) completes; and so on.
If each of these instances of
triangle-recursively are thought
of as different robots, the first robot must wait for the second to
complete its job, which must wait until the third completes, and so
There is a way around this kind of waiting, which we will discuss in Recursion without Deferments.