diff options
author | rsiddharth <rsiddharth@ninthfloor.org> | 2016-03-24 08:20:31 -0400 |
---|---|---|
committer | rsiddharth <rsiddharth@ninthfloor.org> | 2016-03-24 08:20:31 -0400 |
commit | 15cbcf16248f799ccf351623b9f09c8d348ee084 (patch) | |
tree | 6420466b55310d47297b25f19b0959d12b3d773c | |
parent | 59e08dc3c68e13ad1ecbf7e50be5306bc8abaa5a (diff) |
Add one/seven.scm
-rw-r--r-- | one/seven.scm | 30 |
1 files changed, 30 insertions, 0 deletions
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 +;;; <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)))))) |