Initial commit
This commit is contained in:
parent
4e3a048296
commit
9bbad1be58
34
SICP/exercise_1_44.janet
Normal file
34
SICP/exercise_1_44.janet
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
(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)))
|
||||||
Loading…
Reference in New Issue
Block a user