This Text file is old! In a 🏛️Museum, an unsorted archive of (user-)pages. (Saved from Geocities in Oct-2009. The archival story: oocities.org)
--------------------------------------- (To 🚫report any bad content: archivehelp @ gmail.com)
>

; treetravcomb1.scm - n-ary tree traversal combinators

; in a call to foldt (tree fold) different traversals are generated
; by different combinations of list fold (foldl,foldr) and the list
; constructor (cons,cons-reverse) as arguments

(require-library "functio.ss")
; defines 'foldr' and 'foldl'

(define foldt
 (lambda (f g)
  (lambda (tree)
   ;(display (format "~%foldt:~a" tree))  
   (cond 
     ((null? tree) 0) 
     (else 
      (f (car tree) (foldf f g (cdr tree))) )) )))

(define (foldf f g ts)
   ;(display (format "~%foldf:~a" ts))  
   (g (map (foldt f g) ts)) )

(define (cons-reverse x l)
  (append l (list x)))

;-----------------------------------------------------------------------------
(display "depthfirst prefix leftmost")(newline)

(define (df-preleft tree) 
   ((foldt 
     cons 
     (lambda (l) (foldr append '() l))) 
       tree) )

(df-preleft '(1 (2 (5) (6)) (3) (4 (7))) )    ; (1 2 5 6 3 4 7)
(df-preleft '(a (b) (c (d) (e)) (f) ) )       ; (a b c d e f)
;----------------------------------------------------------------
(display "depthfirst prefix rightmost")(newline)

(define (df-preright tree) 
   ((foldt 
     cons 
     (lambda (l) (foldl append '() l))) 
       tree) )

(df-preright '(1 (2 (5) (6)) (3) (4 (7))) )    ; (1 4 7 3 2 6 5)
(df-preright '(a (b) (c (d) (e)) (f) ) )       ; (a f c e d b)
;------------------------------------------------------------------
(display "depthfirst postfix leftmost") (newline)

(define (df-postleft tree) 
   ((foldt 
     cons-reverse 
     (lambda (l) (foldr append '() l))) 
       tree) )

(df-postleft '(1 (2 (5) (6)) (3) (4 (7))) ) ; (5 6 2 3 7 4 1)
(df-postleft '(a (b) (c (d) (e)) (f) ) )    ; (b d e c f a)
;------------------------------------------------------------------
(display "depthfirst postfix rightmost") (newline)

(define (df-postright tree) 
   ((foldt 
     cons-reverse 
     (lambda (l) (foldl append '() l))) 
       tree) )

(df-postright '(1 (2 (5) (6)) (3) (4 (7))) ) ; (7 4 3 6 5 2 1)
(df-postright '(a (b) (c (d) (e)) (f) ) )    ; (f e d c b a) 

Text file Source (historic): geocities.com/soho/square/3472

geocities.com/soho/square
geocities.com/soho

(to report bad content: archivehelp @ gmail)