From: Arthur Flatau

Subject: Re: My clumsy solution to flattening

Date: 1999-3-3 10:14


> 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)))))))
I did not follow this whole, thread, but this does not look a good solution. Here are a few tests using this definition of flatten, which do not produce the results I think you wanted. USER(49): (flatten '(a (b c) d) '((e f))) ; should be (a b c d e f) (A B C D) USER(50): (flatten '(a (b c) d) nil) ; should be (a b c d) NIL USER(51): (flatten 'a nil) ; I think this one is OK NIL USER(52): (flatten nil 'a) ; Oops! Error: Attempt to take the cdr of A which is not listp. [condition type: SIMPLE-ERROR] [1] USER(53): I think your NULL tests above should be ATOM to avoid this error. It is not clear to me whether you want to treat NIL embedded in one of the lists to be considered as a symbol or the empty list, so this may or may not be what you (and your professor) intended: USER(54): (flatten '(nil (b c) d) nil) NIL USER(55): (flatten '(nil (b c) d) '((e f))) (NIL B C D) Art -- Arthur Flatau Texas Microprocessor Division <amd.com at Arthur.Flatau> Advanced Micro Devices 5900 East Ben White Boulevard M/S 625 Austin TX 78741