From: Chris L. G. Hendriks
Subject: LISP pgm segment
Date: 
Message-ID: <31E44654.4F98@intersurf.com>
I am having trouble coding a list traversal segment.
 eg list:

(a (b) (c (e (h i)) (f (j))) (d (g)))

I want to traverse this list in "preorder" and get the following
output:

a b c e h i f j d g

anyone have a code segment to this?

thanks....

Chris.

From: Sunil Mishra
Subject: Re: LISP pgm segment
Date: 
Message-ID: <uloybkq79yv.fsf@Etna.ai.mit.edu>
In article <·············@intersurf.com> "Chris L. G. Hendriks" <·····@intersurf.com> writes:

   From: "Chris L. G. Hendriks" <·····@intersurf.com>
   Newsgroups: comp.lang.lisp
   Date: Wed, 10 Jul 1996 19:09:56 -0500
   Organization: ADEPT
   Path: ai-lab!bloom-beacon.mit.edu!newsfeed.internetmci.com!news.intersurf.net!usenet
   Lines: 15
   Mime-Version: 1.0
   Content-Type: text/plain; charset=us-ascii
   Content-Transfer-Encoding: 7bit
   X-Mailer: Mozilla 2.01 (Win95; I)

   I am having trouble coding a list traversal segment.
    eg list:

   (a (b) (c (e (h i)) (f (j))) (d (g)))

   I want to traverse this list in "preorder" and get the following
   output:

   a b c e h i f j d g

   anyone have a code segment to this?

   thanks....

   Chris.

(defun traverse (l)
  (etypecase l
    (null)
    (cons (traverse (car l))
	  (traverse (cdr l)))
    (atom (print l))))
From: Roberto Olmi
Subject: Re: LISP pgm segment
Date: 
Message-ID: <olmi.23.00097873@iroe.iroe.fi.cnr.it>
In article <·············@intersurf.com> "Chris L. G. Hendriks" <·····@intersurf.com> writes:
>From: "Chris L. G. Hendriks" <·····@intersurf.com>
>Subject: LISP pgm segment
>Date: Wed, 10 Jul 1996 19:09:56 -0500

>I am having trouble coding a list traversal segment.
> eg list:

>(a (b) (c (e (h i)) (f (j))) (d (g)))

>I want to traverse this list in "preorder" and get the following
>output:

>a b c e h i f j d g

>anyone have a code segment to this?

>thanks....

>Chris.

That's another possibility:

(defun flatten-list (L)
  (cond ((null L) nil)
            ((atom (car L)) (cons (car L) (flatten-list (cdr L))))
            (t (append (flatten-list (car L)) (flatten-list (cdr L))))))


Hope this helps,

Roberto Olmi

    
From: Ken Tilton
Subject: Re: LISP pgm segment
Date: 
Message-ID: <31EBD87D.3466@bway.net>
Chris L. G. Hendriks wrote:
> 
> I am having trouble coding a list traversal segment.
>  eg list:
> 
> (a (b) (c (e (h i)) (f (j))) (d (g)))
> 
> I want to traverse this list in "preorder" and get the following
> output:
> 
> a b c e h i f j d g
> 
> anyone have a code segment to this?
> 
> thanks....
> 
> Chris.

Will this work?:

(defun dump (list-or-atom)
   (if (listp list-or-atom)
      (dolist (loa list-or-atom)
          (dump loa))
      (format t "~s " list-or-atom)))

CLOS offers another approach:

(defmethod dump ((l list))
  (dolist (list-or-atom l)
     (dump list-or-atom))

(defmethod dump (atom-or-nil)
  (format t "~s " atom-or-nil)) 

Hope this helps.
From: Eric Lavarde
Subject: Re: LISP pgm segment
Date: 
Message-ID: <31EF77F1.3E9E@hpbbrd.hp.com>
Chris L. G. Hendriks wrote:
> 
> I am having trouble coding a list traversal segment.
>  eg list:
> 
> (a (b) (c (e (h i)) (f (j))) (d (g)))
> 
> I want to traverse this list in "preorder" and get the following
> output:
> 
> a b c e h i f j d g
> 
> anyone have a code segment to this?
> 
> thanks....
> 
> Chris.
Try:

(defun flatten (the-list)
   (mapcan #'(LAMBDA (x) (IF (LISTP x) (flatten x) (LIST x)))
           the-list))
and

(flatten '(a (b) (c (e (h i)) (f (j))) (d (g))))
-> (A B C E H I F J D G)

This functions at least with AKCL, but it is probably not the best
solution as it looks like making a lot of garbage. But well...

Cheers, Eric

-- 
***    If you're interested in Europe, ask me what AEGEE is.     ***

Freiburger Allee 65 * D-71034 B<oe>blingen * Tel: +49-7031/289094
		(<oe> means 'o' with 2 points above it)
HP: ·····@hpbbrd.hp.com Tel: +49-7031/14-4405 Fax: +49-7031/14-2049

***     Ne en France, Studium in Deutschland, being European.    ***
From: Robert Munyer
Subject: Re: LISP pgm segment
Date: 
Message-ID: <4tiodn$595@Mercury.mcs.com>
In article <·············@hpbbrd.hp.com>,
Eric Lavarde  <·····@hpbbrd.hp.com> wrote:

> Try:
>
> (defun flatten (the-list)
>    (mapcan #'(LAMBDA (x) (IF (LISTP x) (flatten x) (LIST x)))
>            the-list))
> and
>
> (flatten '(a (b) (c (e (h i)) (f (j))) (d (g))))
> -> (A B C E H I F J D G)
>
> This functions at least with AKCL, but it is probably not the best
> solution as it looks like making a lot of garbage. But well...

Not giving yourself enough credit.  It makes no garbage at all, as
far as I can see.  I suppose it could with some compilers, but it
shouldn't have to, and it doesn't with mine (MCL).  And it's not
destructive, though at first glance it looks like it might be.

Pretty good for an (almost) one-liner!

-- Robert