From ... From: Erik Naggum Subject: Re: When would it be better to use recursion than a loop? Date: 2000/03/30 Message-ID: <3163396794300015@naggum.no>#1/1 X-Deja-AN: 604254304 References: <38E12D56.F9BB0B50@flashmail.com> mail-copies-to: never Content-Type: text/plain; charset=us-ascii X-Complaints-To: newsmaster@eunet.no X-Trace: oslo-nntp.eunet.no 954407997 23528 195.0.192.66 (30 Mar 2000 09:19:57 GMT) Organization: Naggum Software; vox: +47 8800 8879; fax: +47 8800 8601; http://www.naggum.no User-Agent: Gnus/5.0803 (Gnus v5.8.3) Emacs/20.5 Mime-Version: 1.0 NNTP-Posting-Date: 30 Mar 2000 09:19:57 GMT Newsgroups: comp.lang.lisp * Flounder | I come from a background of C/C++, perl, python, and more languages of | the sort. I am currently learning Lisp on my own and am seeing that Lisp | programmers use recursion more than I have seen in the other languages | that I have programmed in so when is it best to use it and when would it | be best to use loops? Maybe you could just give me some basic rules to | follow on how to decide which to use. here's my rule of thumb: whenever the algorithm generates values from or for each iteration, it is naturally recursive, and will most probably find an elegant recursive expression, which, because it uses an optimized storage of such values, namely the function call stack frame, will also be more efficient than your hand-crafted storage unless you're very good at what you're doing. whenever the algorithm does _not_ generate values from each iteration (as in: generates and consumes an equal amount of values), it is in my not so humble opionion extremely unlikely that a recursive implementation will make more sense than an iterative solution, and not unlikely that a recursive solution will be more complex and will also run slower unless you're careful. #:Erik