From ... From: Erik Naggum Subject: Re: [Q] More beginner help Date: 2000/01/24 Message-ID: <3157705666120505@naggum.no>#1/1 X-Deja-AN: 576887129 References: <9cjn8s4otshmjefp11bi5osigj3o5icf77@4ax.com> mail-copies-to: never Content-Type: text/plain; charset=us-ascii X-Complaints-To: newsmaster@eunet.no X-Trace: oslo-nntp.eunet.no 948718803 27239 195.0.192.66 (24 Jan 2000 13:00:03 GMT) Organization: Naggum Software; +47 8800 8879 or +1 510 435 8604; fax: +47 2210 9077; http://www.naggum.no User-Agent: Gnus/5.0802 (Gnus v5.8.2) Emacs/20.5 Mime-Version: 1.0 NNTP-Posting-Date: 24 Jan 2000 13:00:03 GMT Newsgroups: comp.lang.lisp [ please look at how other Common Lisp programmers indent and present their code. parentheses are _not_ visually significant, although they are very syntactically significant, in Lisp source code. don't make them stand out -- please -- you're code is so ugly it's hard to help you with it. ] * Dirt | I am trying to "unravel" a list, but with little luck. I wish to take a | list such as: | | (a (b c (d e)) f) | | and create a new list that looks like one top level list: | | (a b c d e f) this is normally called "flattening" a list. | I am trying to do this recursively but I am guessing I am way off because | I get a stack overflow. What I am trying to say in my code below is: | | If the expression is a list, call the function again with the cdr of the | list, otherwise it is an atom so cons it to a list. what you wish to do is to move each element of each list you encounter onto a new list. I'll try to show you with slightly more modern Common Lisp that you're using: (defun flatten (list) (loop for element in list if (listp element) nconc (flatten element) else collect element)) if you actually _need_ recursion, which will only waste space and time, you should be able to unravel this iterative solution easily. in my not so humble opinion, being able to think recursively is very valuable, the second most valuable ability you can have, beaten only by knowing _when_ to use recursion. incidentally, this function considers an empty list a list of no elements while you might want NIL to be a separate element in the resulting list. #:Erik