Discussion:
Descending
(too old to reply)
B. Pym
2024-06-07 10:57:15 UTC
Permalink
I am having trouble coding a list traversal segment.
(a (b) (c (e (h i)) (f (j))) (d (g)))
I want to traverse this list in "preorder" and get the following
a b c e h i f j d g
anyone have a code segment to this?
thanks....
Chris.
(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
HenHanna
2024-07-23 19:27:39 UTC
Permalink
Post by B. Pym
I am having trouble coding a list traversal segment.
(a (b) (c (e (h i)) (f (j))) (d (g)))
I want to traverse this list in "preorder" and get the following
a b c e h i f j d g
anyone have a code segment to this?
thanks....
Chris.
(defun dump (list-or-atom)
(if (listp list-or-atom)
(dolist (loa list-or-atom)
(dump loa))
(format t "~s " list-or-atom)))
(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 ?
steve g
2024-08-11 22:51:03 UTC
Permalink
< > 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
< >
Post by HenHanna
(i've added some spaces)
the good ol' Flatten ?
I actually found a reason to use flatten; PDIS systems for rule names!

(defun flatten (l &optional res)
(if (consp l)
(flatten (car l)
(flatten (cdr l) res))
(cons l res)))

(defun generate-flattened-rule-name (pattern)
"Transform <pattern> list into a readable name. Ex (on ?x ? ?y ?z) => ON-?X-?-?Y-?Z ."
(mapconcat #'symbol-name (flatten pattern) "-"))



etc...
HenHanna
2024-08-12 17:29:24 UTC
Permalink
Post by HenHanna
Post by B. Pym
I am having trouble coding a list traversal segment.
(a (b) (c (e (h i)) (f (j))) (d (g)))
I want to traverse this list in "preorder" and get the following
                 a b c e h i f j d g
anyone have a code segment to this?
thanks....
Chris.
(defun dump (list-or-atom)
    (if (listp list-or-atom)
       (dolist (loa list-or-atom)
           (dump loa))
       (format t "~s " list-or-atom)))
(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 ?
is there a convention for printing a Colon ( : ) before something?
Jeff Barnett
2024-08-12 18:33:08 UTC
Permalink
On 8/12/2024 11:29 AM, HenHanna wrote:
<SNIP>
is there a convention for  printing  a Colon ( : )    before  something?
Read up on "packages" in Lisp, particularly about the keyword package.
--
Jeff Ba

Loading...