sicp

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

commit 15cbcf16248f799ccf351623b9f09c8d348ee084
parent 59e08dc3c68e13ad1ecbf7e50be5306bc8abaa5a
Author: rsiddharth <rsiddharth@ninthfloor.org>
Date:   Thu, 24 Mar 2016 08:20:31 -0400

Add one/seven.scm

Diffstat:
one/seven.scm | 30++++++++++++++++++++++++++++++
1 file changed, 30 insertions(+), 0 deletions(-)

diff --git a/one/seven.scm b/one/seven.scm @@ -0,0 +1,30 @@ +;;; Under Creative Commons Attribution-ShareAlike 4.0 +;;; International. See +;;; <https://creativecommons.org/licenses/by-sa/4.0/>. + +(define-module (one seven) + #:export (tolerances-and-sqrt)) + +(use-modules (one six)) + +(define (gen-good-enough tolerance) + (lambda (guess x) + (< (abs (- (square guess) x)) tolerance))) + +(define (sqrt-iter-with-tolerance guess x tolerance) + (let ((good-enough? (gen-good-enough tolerance))) + (if (good-enough? guess x) + guess + (sqrt-iter-with-tolerance (improve guess x) + x tolerance)))) + +(define (sqrt-with-tolerance x tolerance) + (sqrt-iter-with-tolerance 1.0 x tolerance)) + +(define (tolerances-and-sqrt x tolerance) + (let* ((guile-sqrt (sqrt x)) + (custom-sqrt (sqrt-with-tolerance x tolerance))) + (if (eqv? guile-sqrt custom-sqrt) + (cons tolerance custom-sqrt) + (cons (cons tolerance custom-sqrt) + (tolerances-and-sqrt x (/ tolerance 10))))))