Skip to content

Instantly share code, notes, and snippets.

@jfacoustic
Created October 16, 2020 12:58
Show Gist options
  • Save jfacoustic/d64819062ef31836985a96ed45ff258a to your computer and use it in GitHub Desktop.
Save jfacoustic/d64819062ef31836985a96ed45ff258a to your computer and use it in GitHub Desktop.
1.35, 1.36 sicp
(define tolerance 0.00001)
(define (close-enough? v1 v2)
(< (abs (- v1 v2)) tolerance))
(define (fixed-point f first-guess)
(define (try guess)
(let ((next (f guess)))
(if (close-enough? guess next)
next
(try next))))
(try first-guess))
(fixed-point (lambda (x) (+ 1 (/ 1 x))) 1.0) ; 1.6180327868852458
(define (dampened-fixed-point f first-guess)
(define (try guess)
(let ((next (/ (+ guess (f guess)) 2)))
(if (close-enough? guess next)
next
(try next))))
(try first-guess))
(define (func x) (/ (log 1000) (log x)))
(fixed-point func 2.0)
(dampened-fixed-point (lambda (x) (/ (log 1000) (log x))) 2.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment