list版本
(define (make-mobile left right)
(list left right))
(define (make-branch length stucture)
(list length stucture))
(define (left-branch x)
(car x))
(define (right-branch x)
(car (cdr x)))
(define (branch-length x)
(car x))
(define (branch-structure x)
(car (cdr x)))
(define (bsl x) (branch-structure (left-branch x)))
(define (bsr x) (branch-structure (right-branch x)))
(define (total-weight x)
(if (pair? x)
(+ (total-weight (bsl x)) (total-weight (bsr x)))
x))
(define (bll x) (branch-length (left-branch x)))
(define (blr x) (branch-length (right-branch x)))
(define (balance? x)
(cond ((and (pair? (bsl x)) (not (balance? (bsl x)))) #f)
((and (pair? (bsr x)) (not (balance? (bsr x)))) #f)
(else (= (* (bll x) (total-weight (bsl x))) (* (blr x) (total-weight (bsr x)))))))
(define le (make-branch 1 6))
(define re (make-branch 2 3))
(define ae (make-branch 1 (make-mobile le re)))
(define x (make-mobile le re))
(newline)
(display (total-weight x))
(newline)
(display (total-weight (make-mobile re ae)))
(newline)
(display (total-weight (make-mobile ae ae)))
(newline)
(display (balance? (make-mobile ae ae)))
(newline)
(display (balance? (make-mobile le ae)))
将我的程序用cons改写修改了两个地方
(define (right-branch x)
(cdr x))
(define (branch-structure x)
(cdr x))