From 505707dfc55cad17526a26b34bb8e50b5270bc2c Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Wed, 3 Jan 2018 03:18:24 +0000 Subject: net: Add (net ricketyspace sicp one thirtyfive). net/ricketyspace/sicp/one/thirtyfive.scm: New file. --- net/ricketyspace/sicp/one/thirtyfive.scm | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 net/ricketyspace/sicp/one/thirtyfive.scm diff --git a/net/ricketyspace/sicp/one/thirtyfive.scm b/net/ricketyspace/sicp/one/thirtyfive.scm new file mode 100644 index 0000000..04713fa --- /dev/null +++ b/net/ricketyspace/sicp/one/thirtyfive.scm @@ -0,0 +1,28 @@ +;;;; Under Creative Commons Attribution-ShareAlike 4.0 +;;;; International. See +;;;; . + +(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 + -- cgit v1.2.3