Skip to content

Instantly share code, notes, and snippets.

@yao2030
Created December 11, 2012 03:08
Show Gist options
  • Save yao2030/4255620 to your computer and use it in GitHub Desktop.
Save yao2030/4255620 to your computer and use it in GitHub Desktop.
(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