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.)
triangle-recursivelyfunction. We know what happens when Emacs evaluates
triangle-recursivelywith 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 with a smaller argument, until the argument is small
enough so that it does not evaluate itself.
Go to the first, previous, next, last section, table of contents.