diff options
author | rsiddharth <s@ricketyspace.net> | 2018-03-03 18:09:53 +0000 |
---|---|---|
committer | rsiddharth <s@ricketyspace.net> | 2018-03-03 18:09:53 +0000 |
commit | 8839af0454b86a84af00b782bf22de82c67525e3 (patch) | |
tree | 8dcf4416f27f99f34e10f305f2c16a00c4c11d15 /net | |
parent | f7e6dbe6ff8fcbb69e27b9a01bd55f90d65c2642 (diff) |
net: Add (net ricketyspace sicp one forty).
* net/ricketyspace/sicp/one/forty.scm: New file.
Diffstat (limited to 'net')
-rw-r--r-- | net/ricketyspace/sicp/one/forty.scm | 37 |
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)) |