;; compare Program 7.28, pg. 227 ;; In the following I have put leaf? inside the definition of deep-recur, ;; since if clients use deep-recur, they will not need leaf?. (define deep-recur ; TYPE: (-> (T (-> (S T) T) (-> (T T) T)) (-> ((tree S)) T)) (lambda (seed item-proc list-proc) ; ENSURES: (result '()) = seed, ; (result (cons x t)) = (item-proc x (result t)), ; (result (cons t1 t2)) = (list-proc (result t1) (result t2)) ; where x:a and t1,t2:tree of a (letrec ((helper ; TYPE: (-> ((tree S)) T) (lambda (ls) (if (null? ls) seed (let ((a (car ls))) (if (leaf? a) (item-proc a (helper (cdr ls))) (list-proc (helper a) (helper (cdr ls))))) ))) (leaf? ; TYPE: (-> (datum) boolean) (lambda (x) ; ENSURES: result is true iff x is a leaf of a tree (not (or (pair? x) (null? x)))) )) helper)))