summaryrefslogtreecommitdiffstats
path: root/net/ricketyspace
diff options
context:
space:
mode:
Diffstat (limited to 'net/ricketyspace')
-rw-r--r--net/ricketyspace/sicp/one/forty.scm37
1 files changed, 37 insertions, 0 deletions
diff --git a/net/ricketyspace/sicp/one/forty.scm b/net/ricketyspace/sicp/one/forty.scm
new file mode 100644
index 0000000..34d0a26
--- /dev/null
+++ b/net/ricketyspace/sicp/one/forty.scm
@@ -0,0 +1,37 @@
+;;;; Under Creative Commons Attribution-ShareAlike 4.0
+;;;; International. See
+;;;; <https://creativecommons.org/licenses/by-sa/4.0/>.
+
+(define-module (net ricketyspace sicp one forty)
+ #:export (forty cubic))
+
+(define tolerance 0.000000000001)
+(define (fixed-point f first-guess)
+ (define (close-enough? v1 v2)
+ (< (abs (- v1 v2)) tolerance))
+ (define (try guess)
+ (let ((next (f guess)))
+ (if (close-enough? guess next)
+ next
+ (try next))))
+ (try first-guess))
+
+(define dx 0.00001)
+(define (deriv g)
+ (lambda (x)
+ (/ (- (g (+ x dx)) (g x))
+ dx)))
+
+(define (newton-transform g)
+ (lambda (x)
+ (- x (/ (g x) ((deriv g) x)))))
+
+(define (newtons-method g guess)
+ (fixed-point (newton-transform g) guess))
+
+(define (cubic a b c)
+ (lambda (x)
+ (+ (expt x 3) (* a (expt x 2)) (* b x) c)))
+
+(define (forty a b c)
+ (newtons-method (cubic a b c) 1))