sicp

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

commit d0dd460e4934adabcdae7665bf79a8d7d4bea656
parent 70afc89a72a21e3dc41a1072d6ba79bc5380e3ec
Author: rsiddharth <s@ricketyspace.net>
Date:   Sat, 14 Apr 2018 02:33:26 +0000

net: Add (net ricketyspace sicp one fortysix).

* net/ricketyspace/sicp/one/fortysix.scm: New file.

Diffstat:
net/ricketyspace/sicp/one/fortysix.scm | 26++++++++++++++++++++++++++
1 file changed, 26 insertions(+), 0 deletions(-)

diff --git a/net/ricketyspace/sicp/one/fortysix.scm 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))