From: Martti.Halmine (Martti Halminen)

Subject: Re: Flattening brackets

Date: 1999-3-2 4:17

Darren Teemull wrote:
> > Hello, does any one know how to "flatten brackets," ie. if given two > lists as parameters, such as (a (b) c) and (d ((e) f)) I want to get rid > of the brackets around each atomic letter, and just have a list like > either ((a b c) (d e f)) or even better yet, (a b c d e f).?
You can start with these functions from Peter Norvig's book "Paradigms of Artificial Intelligence": (defun mappend (fn list) "Append the results of calling fn on each element of list. Like mapcon, but uses append instead of nconc." (apply #'append (mapcar fn list))) (defun mklist (x) "If x is a list return it, otherwise return the list of x" (if (listp x) x (list x))) (defun flatten (exp) "Get rid of imbedded lists (one level only)." (mappend #'mklist exp)) Then just write something to repeat this until the result only contains atoms. Combining several results is just a call to append etc. -- ________________________________________________________________ ^. Martti Halminen / \`. Design Power Europe Oy / \ `. Tekniikantie 12, FIN-02150 Espoo, Finland /\`. \ | Tel:+358 9 4354 2306, Fax:+358 9 455 8575 /__\|___\| <dpe.fi at Mailto:Martti.Halminen> http://www.dpe.fi