William James
5/4/2015 3:56:00 AM
Ken Tilton wrote:
> 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)))
Gauche Scheme:
(define (dump x)
(if (pair? x)
(for-each dump x)
(display #"~x ")))
(dump '(a (b) (c (e (h i)) (f (j))) (d (g))))
===>
a b c e h i f j d g #<undef>
> 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))
(define-method dump ((x <list>))
(for-each dump x))
(define-method dump (x)
(display #"~x "))
(dump '(a (b) (c (e (h i)) (f (j))) (d (g))))
===>
a b c e h i f j d g #<undef>
--
Sweden ... has had more immigration during the past years than almost any other
Western nation. Two out of three new Swedes are actually immigrants....
Today, out of a population of 9 million people, about 1 million are born
outside Sweden. This constitutes 11% of the population. If we also include
their children born in Sweden, the figure rises to nearly 20%.
fjordman.blogspot.ca/2005/05/is-swedish-democracy-collapsing.html