From ... Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news.tele.dk!small.news.tele.dk!141.201.2.63!newshunter!cosy.sbg.ac.at!newsfeed.Austria.EU.net!newsfeed.kpnqwest.at!nslave.kpnqwest.net!nloc.kpnqwest.net!nmaster.kpnqwest.net!nreader1.kpnqwest.net.POSTED!not-for-mail Newsgroups: comp.lang.lisp Subject: Re: How to reverse a list... References: <5b829932.0112030950.3f96120b@posting.google.com> <3c0bf442$0$200$626a54ce@news.free.fr> Mail-Copies-To: never From: Erik Naggum Message-ID: <3216414699382977@naggum.net> Organization: Naggum Software, Oslo, Norway Lines: 35 User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.1 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Date: Tue, 04 Dec 2001 00:31:41 GMT X-Complaints-To: newsmaster@KPNQwest.no X-Trace: nreader1.kpnqwest.net 1007425901 193.71.66.49 (Tue, 04 Dec 2001 01:31:41 MET) NNTP-Posting-Date: Tue, 04 Dec 2001 01:31:41 MET Xref: archiver1.google.com comp.lang.lisp:21814 * "Frederic Brunel" | In my opinion, an elegant and typical functional way to do this is to use | an auxiliary function taking a second argument to accumulate the result: | | (defun reverse-aux (l acc) | (if (nilp l) | acc | (reverse-aux (cdr l) | (append (list (car l)) acc)))) | | (defun my-reverse (l) | (reverse-aux l '())) | | You could use the `label' special-form to declare the `reverse-aux' | function as a local function within the `my-reverse' function or set the | `acc' argument as an optional one! That would probably be elegant only in Scheme. Please note that (append (list elt) list) is functionally identical to (cons elt list), except that it copies and wastes the first argument. This is another way to do it: (defun reverse-list (list) (do ((list list (cdr list)) (rev () (cons (car list) rev))) ((endp list) rev))) /// -- The past is not more important than the future, despite what your culture has taught you. Your future observations, conclusions, and beliefs are more important to you than those in your past ever will be. The world is changing so fast the balance between the past and the future has shifted.