Newsgroups: comp.lang.lisp
Path: cantaloupe.srv.cs.cmu.edu!bb3.andrew.cmu.edu!newsfeed.pitt.edu!gatech!newsfeed.internetmci.com!howland.reston.ans.net!ix.netcom.com!netcom.com!NewsWatcher!user
From: hbaker@netcom.com (Henry Baker)
Subject: Re: clever flatten?
Message-ID: <hbaker-0902961757550001@10.0.2.15>
Sender: hbaker@netcom12.netcom.com
Organization: nil organization
References: <Ul6vo1C00iUvE7tL4d@andrew.cmu.edu>
Date: Sat, 10 Feb 1996 01:57:55 GMT
Lines: 23

In article <Ul6vo1C00iUvE7tL4d@andrew.cmu.edu>, "Robert G. Malkin"
<rm6k+@andrew.cmu.edu> wrote:

> does anyone know of a clever list flattening routine?
> that is,
> (flatten '((ab) nil (a (bcd)) (df))) -> (ab a bcd df)
> 
> the cl implementations i am using don't have series, so i can't just
> call (collect-append (scan list)). i'm using flatten for some pretty big
> lists (50-100k) and its inefficiency blows up space requirements to the
> point where its use is becoming impractical... any suggestions?
> thanks

(defun flatten (x) (flatten-helper x nil))

(defun flatten-helper (x r)      ;;; 'r' is the stuff to the 'right'.
  (cond ((atom x) (cons x r))
        (t (flatten-helper (car x) (flatten-helper (cdr x) r)))))

-- 
www/ftp directory:
ftp://ftp.netcom.com/pub/hb/hbaker/home.html

