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)))
Ken Tilton 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 list-or-atom)
(cond ((null? list-or-atom) )
((list? list-or-atom)
(begin
(dump (car list-or-atom))
(dump (cdr list-or-atom))))
(#t (format #t ":~s " list-or-atom))))
(dump '(a (b) (c (e (h i)) (f (j))) (d (g))))
===>
:a :b :c :e :h :i :f :j :d :g #t
On 6/7/2024 3:57 AM, B. Pym wrote:
Ken Tilton 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 list-or-atom)
(cond ((null? list-or-atom) )
((list? list-or-atom)
(begin
(dump (car list-or-atom))
(dump (cdr list-or-atom))))
(#t (format #t ":~s " list-or-atom))))
(dump '(a (b) (c (e (h i)) (f (j))) (d (g))))
===>
:a :b :c :e :h :i :f :j :d :g #t
(i've added some spaces)
the good ol' Flatten ?
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 417 |
Nodes: | 16 (2 / 14) |
Uptime: | 05:33:44 |
Calls: | 8,757 |
Files: | 13,285 |
Messages: | 5,963,065 |