From 8839af0454b86a84af00b782bf22de82c67525e3 Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Sat, 3 Mar 2018 18:09:53 +0000 Subject: net: Add (net ricketyspace sicp one forty). * net/ricketyspace/sicp/one/forty.scm: New file. --- net/ricketyspace/sicp/one/forty.scm | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 net/ricketyspace/sicp/one/forty.scm 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 +;;;; . + +(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)) -- cgit v1.2.3