From b0e69993807380ca9dcecb00533bd20f953c0b25 Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Mon, 15 Jul 2019 17:46:05 -0400 Subject: net/ricketyspace/sicp/two/twentynine.scm: Update total-weight. * net/ricketyspace/sicp/two/twentynine.scm (total-weight): Fix function. --- net/ricketyspace/sicp/two/twentynine.scm | 50 +++++++++++++++++--------------- 1 file changed, 27 insertions(+), 23 deletions(-) (limited to 'net/ricketyspace') diff --git a/net/ricketyspace/sicp/two/twentynine.scm b/net/ricketyspace/sicp/two/twentynine.scm index 2340f38..073f55e 100644 --- a/net/ricketyspace/sicp/two/twentynine.scm +++ b/net/ricketyspace/sicp/two/twentynine.scm @@ -31,31 +31,35 @@ (define (branch-weight b) (let ((bs (branch-structure b))) (cond ((not (pair? bs)) bs) - (else (branch-weight bs))))) + (else (+ (branch-weight (left-branch bs)) + (branch-weight (right-branch bs))))))) (+ (branch-weight (left-branch mobile)) (branch-weight (right-branch mobile)))) ;;; Guile REPL ;;; -;;; scheme@(guile-user)> ,re (net ricketyspace sicp two twentynine) -;;; scheme@(guile-user)> (make-mobile (make-branch 3 45) (make-branch 4 (make-branch 5 50))) -;;; $4 = ((3 45) (4 (5 50))) -;;; scheme@(guile-user)> (left-branch $4) -;;; $5 = (3 45) -;;; scheme@(guile-user)> (right-branch $4) -;;; $6 = (4 (5 50)) -;;; scheme@(guile-user)> (branch-length (left-branch $4)) -;;; $7 = 3 -;;; scheme@(guile-user)> (branch-structure (left-branch $4)) -;;; $8 = 45 -;;; scheme@(guile-user)> (branch-length (right-branch $4)) -;;; $9 = 4 -;;; scheme@(guile-user)> (branch-structure (right-branch $4)) -;;; $10 = (5 50) -;;; scheme@(guile-user)> (branch-length (branch-structure (right-branch $4))) -;;; $11 = 5 -;;; scheme@(guile-user)> (branch-structure (branch-structure (right-branch $4))) -;;; $12 = 50 -;;; scheme@(guile-user)> (total-weight $4) -;;; $14 = 95 - +;;; scheme@(guile-user)> (make-mobile (make-branch 3 45) +;;; (make-branch 4 +;;; (make-mobile (make-branch 5 50) +;;; (make-branch 1 20)))) +;;; $11 = ((3 45) (4 ((5 50) (1 20)))) +;;; scheme@(guile-user)> (left-branch $11) +;;; $12 = (3 45) +;;; scheme@(guile-user)> (right-branch $11) +;;; $13 = (4 ((5 50) (1 20))) +;;; scheme@(guile-user)> (branch-length (left-branch $11)) +;;; $14 = 3 +;;; scheme@(guile-user)> (branch-structure (left-branch $11)) +;;; $15 = 45 +;;; scheme@(guile-user)> (branch-length (right-branch $11)) +;;; $16 = 4 +;;; scheme@(guile-user)> (branch-structure (right-branch $11)) +;;; $17 = ((5 50) (1 20)) +;;; scheme@(guile-user)> (branch-structure (right-branch $11)) +;;; $18 = ((5 50) (1 20)) +;;; scheme@(guile-user)> (left-branch (branch-structure (right-branch $11))) +;;; $19 = (5 50) +;;; scheme@(guile-user)> (right-branch (branch-structure (right-branch $11))) +;;; $20 = (1 20) +;;; scheme@(guile-user)> (total-weight $11) +;;; $21 = 115 -- cgit v1.2.3