Loading

foldts.scm

  1.  
  2. ;;
  3. ;; (foldts fdown fup fhere seed)
  4. ;;
  5. ;; - fhere is applied to the leafs of the tree
  6. ;;
  7. ;; - fdown is invoked when a non-leaf node is entered before any of
  8. ;; the node's children are visited. fdown action has to generate a
  9. ;; seed to be passed to the first visited child of the node.
  10. ;;
  11. ;; - fup is invoked after all the children of a node have been
  12. ;; seen. The first argument is the local state at the moment the
  13. ;; traversal process enters the branch rooted at the current node. The
  14. ;; second argument is the result of visiting all child branches.  The
  15. ;; action of fup isto produce a seed that is taken to be the state of
  16. ;; the traversal after the process leave the currents the current
  17. ;; branch.
  18.  
  19. (define (foldts fdown fup fhere seed tree)
  20.   (cond
  21.    ((null? tree) seed)
  22.    ((not (pair? tree))      ; An atom
  23.     (fhere seed tree))
  24.    (else
  25.     (let loop ((kid-seed (fdown seed tree))
  26.                (kids (cdr tree)))
  27.       (if (null? kids)
  28.       (fup seed kid-seed tree)
  29.       (loop (foldts fdown fup fhere kid-seed (car kids))
  30.         (cdr kids)))))))