(define (make-mobile left right) (list left right))(define mobile (make-mobile left right)) (define (make-branch length structure) (list length structure)) (define branch (make-branch length structure)) (define (left-branch mobile) (car mobile)) (define (right-branch mobile) (cadr mobile)) (define (branch-length branch) (car branch)) (define (branch-structure branch) (cadr branch)) (define (total-weight mobile) (+ (branch-weight (left-branch mobile)) (branch-weight (right-branch mobile)))) (define (branch-weight branch) (if (pair? (branch-structure branch)) (total-weight (branch-structure branch)) (branch-structure branch))) (define (branch-torque branch) (* (branch-length branch) (branch-weight branch))) (define (mobile-balance? mobile) (let ((left (left-branch mobile)) (right (right-branch mobile))) (and ; 必须同时满足以下三个条件,才是平衡的活动体 (same-torque? left right) (branch-balance? left) (branch-balance? right)))) (define (same-torque? left right) (= (branch-torque left) (branch-torque right))) (define (branch-balance? branch) (if (hangs-another-mobile? branch) ; 如果分支上有子活动体 (mobile-balance? (branch-structure branch)) ; 那么(递归地)检查子活动体的平衡性 #t)) ; 否则,返回 #t true
如果将 (define (make-mobile left right)(cons left right))
那么 (define (left-mobile mobile) (car mobile)) (define (right-mobile mobile) (cdr mobile))list 和 cons 的区别, 一个简单的例子: (list a b c) = (cons a (cons b (cons c '())))