diff options
author | rsiddharth <s@ricketyspace.net> | 2018-04-14 02:33:26 +0000 |
---|---|---|
committer | rsiddharth <s@ricketyspace.net> | 2018-04-14 02:33:26 +0000 |
commit | d0dd460e4934adabcdae7665bf79a8d7d4bea656 (patch) | |
tree | 6bfa5d2af9694764ae11ce01ed20ca9d23e44828 | |
parent | 70afc89a72a21e3dc41a1072d6ba79bc5380e3ec (diff) |
net: Add (net ricketyspace sicp one fortysix).
* net/ricketyspace/sicp/one/fortysix.scm: New file.
-rw-r--r-- | net/ricketyspace/sicp/one/fortysix.scm | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/net/ricketyspace/sicp/one/fortysix.scm b/net/ricketyspace/sicp/one/fortysix.scm new file mode 100644 index 0000000..b97945d --- /dev/null +++ b/net/ricketyspace/sicp/one/fortysix.scm @@ -0,0 +1,26 @@ +;;;; Under Creative Commons Attribution-ShareAlike 4.0 +;;;; International. See +;;;; <https://creativecommons.org/licenses/by-sa/4.0/>. +;;;; + +(define-module (net ricketyspace sicp one fortysix) + #:export (fixed-point fp-sqrt)) + +(define (iterative-improve good-enough? improve) + (define (try guess) + (let ((next (improve guess))) + (if (good-enough? guess next) + next + (try next)))) + try) + +(define (fixed-point f guess) + (define tolerance 0.00001) + (define (close-enough? v1 v2) + (< (abs (- v1 v2)) tolerance)) + ((iterative-improve close-enough? f) guess)) + +(define (fp-sqrt x) + (define (average x y) (/ (+ x y) 2.0)) + (fixed-point (lambda (y) (average y (/ x y))) + 1.0)) |