chickadee » sxml-transforms » foldts

foldts fdown fup fhere seed treeprocedure

Tree fold operator.

   tree = atom | (node-name tree ...)
   foldts fdown fup fhere seed (Leaf str) = fhere seed str
   foldts fdown fup fhere seed (Nd kids) =
         fup seed $ foldl (foldts fdown fup fhere) (fdown seed) kids
   procedure fhere: seed -> atom -> seed
   procedure fdown: seed -> node -> seed
   procedure fup: parent-seed -> last-kid-seed -> node -> seed

foldts returns the final seed

Here is an identity implementation to get you started:

(foldts
  (lambda (seed node) ; fdown
    (list (car node)))
  (lambda (parent-seed lk-seed node) ; fup
    (if (null? parent-seed)
      (reverse lk-seed)
      (cons (reverse lk-seed) parent-seed)))
  (lambda (seed atom) ; fhere
    (cons atom seed))
  '()
  '(div o ii (p "my paragraph" "twosies") j (p "another one") (hr) (div (p "hello world") "chips")))