From 15cbcf16248f799ccf351623b9f09c8d348ee084 Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Thu, 24 Mar 2016 08:20:31 -0400 Subject: Add one/seven.scm --- one/seven.scm | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 one/seven.scm (limited to 'one/seven.scm') diff --git a/one/seven.scm b/one/seven.scm new file mode 100644 index 0000000..66e06d9 --- /dev/null +++ b/one/seven.scm @@ -0,0 +1,30 @@ +;;; Under Creative Commons Attribution-ShareAlike 4.0 +;;; International. See +;;; . + +(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)))))) -- cgit v1.2.3