From d0dd460e4934adabcdae7665bf79a8d7d4bea656 Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Sat, 14 Apr 2018 02:33:26 +0000 Subject: net: Add (net ricketyspace sicp one fortysix). * net/ricketyspace/sicp/one/fortysix.scm: New file. --- net/ricketyspace/sicp/one/fortysix.scm | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 net/ricketyspace/sicp/one/fortysix.scm (limited to 'net/ricketyspace') 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 +;;;; . +;;;; + +(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)) -- cgit v1.2.3