summaryrefslogtreecommitdiffstats
path: root/net/ricketyspace
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2018-01-03 03:18:24 +0000
committerrsiddharth <s@ricketyspace.net>2018-01-03 03:18:24 +0000
commit505707dfc55cad17526a26b34bb8e50b5270bc2c (patch)
tree00d86dc3e5bdda676de5a8d30b9e7cbac74442ef /net/ricketyspace
parentf79c9c2fd720c24a2b94b5e6b5950b1e6fb414e1 (diff)
net: Add (net ricketyspace sicp one thirtyfive).
net/ricketyspace/sicp/one/thirtyfive.scm: New file.
Diffstat (limited to 'net/ricketyspace')
-rw-r--r--net/ricketyspace/sicp/one/thirtyfive.scm28
1 files changed, 28 insertions, 0 deletions
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
+;;;; <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
+