sicp

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

commit 8839af0454b86a84af00b782bf22de82c67525e3
parent f7e6dbe6ff8fcbb69e27b9a01bd55f90d65c2642
Author: rsiddharth <s@ricketyspace.net>
Date:   Sat,  3 Mar 2018 18:09:53 +0000

net: Add (net ricketyspace sicp one forty).

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

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

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