Learning/SICP/exercise_1_44.janet
2025-06-03 20:47:33 +02:00

35 lines
772 B
Plaintext

(defn compose [f g]
(fn [x] (f (g x))))
(defn repeated [f n]
(defn iter [inner_f k]
(cond
(= k 1) inner_f
(iter (compose f inner_f) (- k 1))))
(iter f n))
(def dx 0.01)
(defn smooth [f]
(fn [x] (let [c (f x)
p (f (- x dx))
n (f (+ x dx))]
(/ (+ c p n) 3))))
(defn n-smooth [f n]
((repeated smooth n) f))
(defn cubic [a b c] (fn [x] (+ (* x x x) (* a x x) (* b x) c)))
(def abs-cube (compose math/abs math/sin))
(def smooth-abs (smooth abs-cube))
(def n-smooth-abs (n-smooth abs-cube 5))
(var i 0)
(while (< i 400000)
(printf "%f,%f,%f,%f"
(* i 0.00001)
(abs-cube (* i 0.00001))
(smooth-abs (* i 0.00001))
(n-smooth-abs (* i 0.00001)))
(set i (+ i 1)))