<< Previous exercise (2.28) | Index | Next exercise (2.30) >>

Use the same recursion over the construction of the mobile for defining the function 'total-weight' as; well as the predicate 'balanced?'. Take advantage of the fact that a variable has no type. The variab;le 'm' can in different calls take as a value a mobile, a branch or a number or empty list.

; Definition of constructors and selectors (define (make-mobile left right) (list left right)) (define (left-branch mobile) (car mobile)) (define (right-branch mobile) (car (cdr mobile))) (define (make-branch length structure) (list length structure)) (define (branch-length branch) (car branch)) (define (branch-structure branch) (car (cdr branch))) ;; Redefinition of constructors and selectors (define (make-mobile left right) (cons left right)) (define (left-branch mobile) (car mobile)) (define (right-branch mobile) (cdr mobile)) (define (make-branch length structure) (cons length structure)) (define (branch-length branch) (car branch)) (define (branch-structure branch) (cdr branch)) (define (total-weight m) (cond ((null? m) 0) ((not (pair? m)) m) (else (+ (total-weight (branch-structure (left-branch m))) (total-weight (branch-structure (right-branch m))))))) (define m1 (make-mobile (make-branch 4 6) (make-branch 5 (make-mobile (make-branch 3 7) (make-branch 9 8))))) ;; 4 | 5 ;; +----+-----+ ;; 6 3 | 9 ;; +---+---------+ ;; 7 8 ; (total-weight m1) ; Value: 21 (define (balanced? m) (cond ((null? m) #t) ((not (pair? m)) #t) (else (and (= (* (branch-length (left-branch m)) (total-weight (branch-structure (left-branch m)))) (* (branch-length (right-branch m)) (total-weight (branch-structure (right-branch m))))) (balanced? (branch-structure (left-branch m))) (balanced? (branch-structure (right-branch m))))))) (define m2 (make-mobile (make-branch 4 6) (make-branch 2 (make-mobile (make-branch 5 8) (make-branch 10 4))))) ;; 4 | 2 ;; +----+--+ ;; 6 5 | 10 ;; +-----+----------+ ;; 8 4 (balanced? m2) ;Value: #t (balanced? m1) ;Value: #f

Making the constructors and selectors first for building the bigger solution on.

Version one using lists

Version two using cons instead of lists

Rest of the problem

The test cases were used from Bill the Lizard's blog on SICP