sicp

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

commit e40d222c1be0693f44d3f15372a46598ee39c31b
parent 0bfe90e1fd4182d489a4933702a4ccaef3fd6eeb
Author: rsiddharth <s@ricketyspace.net>
Date:   Sat, 25 Aug 2018 15:03:05 +0000

Add (net ricketyspace sicp two twelve).

* net/ricketyspace/sicp/two/twelve.scm: New file.

Diffstat:
net/ricketyspace/sicp/two/twelve.scm | 40++++++++++++++++++++++++++++++++++++++++
1 file changed, 40 insertions(+), 0 deletions(-)

diff --git a/net/ricketyspace/sicp/two/twelve.scm b/net/ricketyspace/sicp/two/twelve.scm @@ -0,0 +1,40 @@ +;;;; Under Creative Commons Attribution-ShareAlike 4.0 +;;;; International. See +;;;; <https://creativecommons.org/licenses/by-sa/4.0/>. + +(define-module (net ricketyspace sicp two twelve) + #:use-module (net ricketyspace sicp two seven) + #:export (make-center-width + make-center-percent + center + width + percent)) + +(define (make-center-width c w) + (make-interval (- c w) (+ c w))) + +(define (make-center-percent c p) + (define (pof p c) (* c (/ p 100.0))) + (make-interval (- c (pof p c)) + (+ c (pof p c)))) + +(define (center i) + (/ (+ (lower-bound i) (upper-bound i)) 2)) +(define (width i) + (/ (- (upper-bound i) (lower-bound i)) 2)) + +(define (center i) + (/ (+ (lower-bound i) (upper-bound i)) 2)) + +(define (width i) + (/ (- (upper-bound i) (lower-bound i)) 2)) + +(define (percent i) + (* (/ (width i) (center i)) 100)) + +;;; Guile REPL +;;; +;;; scheme@(guile-user)> (percent (make-center-percent 45 15)) +;;; $16 = 15.0 +;;; scheme@(guile-user)> (percent (make-center-percent 40 16)) +;;; $17 = 15.999999999999998