Learning/SICP/exercise_1_36.janet
2025-05-29 22:00:25 +02:00

29 lines
641 B
Plaintext

(def tolerance 0.00001)
(defn fixed-point [f damping first-guess]
(defn close-enough? [v1 v2]
(< (math/abs (- v1 v2))
tolerance))
(defn try-it [guess]
(printf "current guess %f" guess)
(let [next (damping guess (f guess))]
(if (close-enough? guess next)
next
(try-it next))))
(try-it first-guess))
(print "undamped")
(print (fixed-point
(fn [x] (/ (math/log 1000) (math/log x)))
(fn [x1 x2] x2)
2.0
)
)
(print "damped")
(print (fixed-point
(fn [x] (/ (math/log 1000) (math/log x)))
(fn [x1 x2] (* 0.5 (+ x1 x2)))
2.0
)
)