Created
December 11, 2012 03:08
-
-
Save yao2030/4255620 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (make-mobile left right) | |
(list left right)) | |
(define (make-branch length structure) | |
(list length structure)) | |
(define (left-branch x) | |
(car x)) | |
(define (right-branch x) | |
(cadr x)) | |
(define (branch-length x) | |
(car x)) | |
(define (branch-structure x) | |
(cadr x)) | |
(define (total-weight x) | |
(cond ((null? x) 0) | |
((not (pair? x)) x) | |
(else (+ (total-weight (left-branch x)) | |
(total-weight (right-branch x)))))) | |
(define (balanced? x) | |
(cond ((null? x) #t) | |
((= (* (branch-length (left-branch x)) (total-weight (left-branch x))) | |
(* (branch-length (right-branch x)) (total-weight (right-branch x)))) | |
(and (balanced? (left-branch x)) | |
(balanced? (right-branch x)))) | |
(else #f))) | |
;; d | |
cadr -> cdr | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment