sicp

sicp sandbox.
git clone git://git.ricketyspace.net/sicp.git
Log | Files | Refs

commit 6bf176f3e091328ceb43de82b6514a23ccef755e
parent a2fc1fda91fbae99f3f14b3370ccd67871ac6c87
Author: rsiddharth <s@ricketyspace.net>
Date:   Mon, 15 Jul 2019 19:21:01 -0400

two/twentynine.scm: Add mobile-balanced?

* net/ricketyspace/sicp/two/twentynine.scm (has-submobile?)
(torque, mobile-balanced?): New functions.

Diffstat:
net/ricketyspace/sicp/two/twentynine.scm | 22+++++++++++++++++++++-
1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/net/ricketyspace/sicp/two/twentynine.scm b/net/ricketyspace/sicp/two/twentynine.scm @@ -7,7 +7,8 @@ right-branch branch-length branch-structure - total-weight)) + total-weight + mobile-balanced?)) (define (make-mobile left right) (list left right)) @@ -37,6 +38,25 @@ (+ (branch-weight (left-branch mobile)) (branch-weight (right-branch mobile)))) +(define (has-submobile? branch) + (not (number? (branch-structure branch)))) + +(define (torque branch) + (* (branch-length branch) + (branch-weight branch))) + +(define (mobile-balanced? m) + (let ((lb (left-branch m)) + (rb (right-branch m))) + (and + (cond ((has-submobile? lb) + (mobile-balanced? (branch-structure lb))) + (else #t)) + (cond ((has-submobile? rb) + (mobile-balanced? (branch-structure rb))) + (else #t)) + (= (torque lb) (torque rb))))) + ;;; Guile REPL ;;; ;;; scheme@(guile-user)> (make-mobile