From: Rob Farrow

Subject: Re: My clumsy solution to flattening

Date: 1999-3-3 10:41


These principals are true of programming in general, regardless of
language.  Is this a forum for teaching beginning programming?

I hope not.

Rob Farrow                             || There is no ultimate answer.
Phone: 972/480-2698 MSGID: RCFJ        || Only more revealing ways
Email: <ti.com at farrow>                   || of looking at the question.


Martti Halminen writes:
> Darren Teemull wrote:
> > > > Thanks for all the suggestions from the people who responded to my > > earlier questions. This is what I finally came up with (Sorry, but I > > don't think it matches anyone's suggestion, but that's simply because of > > my ineptness at Lisp) feel free to rip it apart and improve on it if you > > like. > > > > Solution to the flattening and combining of two lists, each containing > > any amount of nested lists and brackets (eg. 1=((a b) c) & 2=(d ((e) f)) > > then (flatten 1 2) = (a b c d e f)) : > > > > (defun flatten (fl1 fl2) > > (cond ((null fl2) NIL) > > (t (cond ((null fl1) (flatten fl2 (cdr fl2))) > > ((listp (car fl1)) > > (cons (car (flatten (car fl1) fl2)) > > (if (null (cdr (car fl1))) > > (flatten (cdr fl1) fl2) > > (flatten (cons (cdr (car fl1)) (cdr > > fl1)) fl2)))) > > (t (cons (car fl1) (flatten (cdr fl1) > > fl2)))))))
> > > There are some typical beginner's problems here: odd indentation, > unnecessary cond calls (why not take "(t (cond" away from the third > line?), and some odd behaviour: try (flatten '(some list) nil), what was > the intended result? > > But the main problem here is architectural: you get unnecessary > complication trying to do 2 things (flattening and combining lists) at > the same time; especially as the case of 2 lists is rather arbitrary. It > would be simpler to write a separate function to flatten one list, and > another function to combine them. > > for example: > > (defun flatten (list) > "Flattens a list: (a b (c d (e)) f) -> (a b c d e f)" > ;; Write something here to do the job... > ) > > (defun flatten2 (a b) > "Flattens 2 lists and combines them" > (append (flatten a) (flatten b))) > > (defun flatten-n (&rest lists) > "Combines its arguments to one list and flattens it" > (mapcan #'flatten lists)) > > > One of the common errors both beginning students and professors only > dabbling in lisp make is trying to restrict themselves to some arbitrary > "pure" subset of lisp, thereby re-inventing the wheel again and again. > > If the idea is to learn using CL productively, you should utilize > whatever machinery exists in the language and leave the low-level > implementation to the implementors. One of the major strengths of CL is > the large "library" of useful stuff already defined in the language. > > Re-building existing stuff makes very little sense after the principles > have been learned. > > -- > ________________________________________________________________ > ^. 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 >