(define atomic-item? ; TYPE: (-> (datum) boolean) (lambda (x) (not (or (pair? x) (null? x))))) (define atomic-items ; TYPE: (-> ((tree atomic-item)) ; (list atomic-item)) (lambda (tr) ; ENSURES: result is a list of the ; atomic items in tr (cond ((null? tr) '()) ((atomic-item? (car tr)) (cons (car tr) (atomic-items (cdr tr)))) (else (append (atomic-items (car tr)) (atomic-items (cdr tr))))))) (define add1-to-all ; TYPE: (-> ((tree number)) ; (tree number)) (lambda (tr) ; REQUIRES: atoms in tr are numbers (cond ((null? tr) '()) ((number? (car tr)) (cons (add1 (car tr)) (add1-to-all (cdr tr)))) (else (cons (add1-to-all (car tr)) (add1-to-all (cdr tr))))))) (define sum-all ; TYPE: (-> ((tree number)) number) (lambda (ton) ; ENSURES: result is the sum of ; all the numbers in ton (cond ((null? ton) 0) ((number? (car ton)) (+ (car ton) (sum-all (cdr ton)))) (else (+ (sum-all (car ton)) (sum-all (cdr ton))))))) (define tree-neg ; TYPE: (-> ((tree atomic-item)) ; (tree atomic-item)) (lambda (tr) (cond ((null? tr) '()) ((atomic-item? (car tr)) (cons (car tr) (tree-neg (cdr tr)))) (else (cons (tree-neg (car tr)) (tree-neg (cdr tr))))))) (define count-atomic-items ; TYPE: (-> ((tree atomic-item)) number) (lambda (ls) ; REQUIRES: atoms in ls are numbers (cond ((null? ls) 0) ((atomic-item? (car ls)) (add1 (count-atomic-items (cdr ls))) ) (else (+ (count-atomic-items (car ls)) (count-atomic-items (cdr ls)))) ))) (define atom? (lambda (datum) (not (pair? datum)))) (define count-all ; TYPE: (-> ((tree atom)) number) (lambda (ls) ; REQUIRES: atoms in ls are numbers (cond ((null? ls) 0) ((atom? (car ls)) (add1 (count-all (cdr ls)))) (else (+ (count-all (car ls)) (count-all (cdr ls)))))))