sicp

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

commit 505707dfc55cad17526a26b34bb8e50b5270bc2c
parent f79c9c2fd720c24a2b94b5e6b5950b1e6fb414e1
Author: rsiddharth <s@ricketyspace.net>
Date:   Wed,  3 Jan 2018 03:18:24 +0000

net: Add (net ricketyspace sicp one thirtyfive).

net/ricketyspace/sicp/one/thirtyfive.scm: New file.

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

diff --git a/net/ricketyspace/sicp/one/thirtyfive.scm b/net/ricketyspace/sicp/one/thirtyfive.scm @@ -0,0 +1,28 @@ +;;;; Under Creative Commons Attribution-ShareAlike 4.0 +;;;; International. See +;;;; <https://creativecommons.org/licenses/by-sa/4.0/>. + +(define-module (net ricketyspace sicp one thirtyfive) + #:export (golden-ratio)) + +(define tolerance 0.00001) + +(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 (golden-ratio ) + (fixed-point (lambda (x) (+ 1 (/ 1.0 x))) 1.0)) + +;;; Guile REPL +;;; +;;; scheme@(guile-user)> ,use (net ricketyspace sicp one thirtyfive) +;;; scheme@(guile-user)> (golden-ratio) +;;; $1 = 1.6180327868852458 +