www.delorie.com/gnu/docs/emacs-lisp-intro/emacs-lisp-intro_174.html | search |
Buy the book! | |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The triangle
function described in a previous section can also
be written recursively. It looks like this:
(defun triangle-recursively (number) "Return the sum of the numbers 1 through NUMBER inclusive. Uses recursion." (if (= number 1) ; do-again-test 1 ; then-part (+ number ; else-part (triangle-recursively ; recursive call (1- number))))) ; next-step-expression (triangle-recursively 7) |
You can install this function by evaluating it and then try it by
evaluating (triangle-recursively 7)
. (Remember to put your
cursor immediately after the last parenthesis of the function
definition, before the comment.) The function evaluates to 28.
To understand how this function works, let's consider what happens in the various cases when the function is passed 1, 2, 3, or 4 as the value of its argument.
An argument of 1 or 2 An argument of 3 or 4
webmaster | delorie software privacy |
Copyright © 2003 by The Free Software Foundation | Updated Jun 2003 |