Что здесь происходит? (ns test0.core (:gen-class) ;;(require [clojure.test :as t]) )…
Jump to turn (7)
- 1Что здесь происходит? (ns test0.core (:gen-class) ;;(require [clojure.test :as t
- 2А мы можем создать похожую функцию, но без мемоизации? То есть идентичную, без м
- 3Не совсем. Я хочу в одном исходном коде иметь сразу две версии и в main их уже с
- 4А чем отличается эта версия от первоначальной? (def integrate (fn [memo-f h summ
- 5Давай остановимся на второй версии в качестве конечной для мемоизированной функц
- 6А что нового в этой версии? (ns test0.core (:gen-class) ;;(require [clojure.test
- 7А мы можем теперь написать такой код, в котором бы было сравнение мемоизированно
- User
Что здесь происходит? (ns test0.core (:gen-class) ;;(require [clojure.test :as t]) )
(def h 0.01)
(defn trapezoid [f h x1 x2] (* h (/ (+ (f x1) (f x2)) 2)))
(def intergate (memoize (fn [f h x] (let [x1 (* h x), x2 (* h (dec x))] (if (zero? x) 0 (+ (trapezoid f h x1 x2) (intergate f h (dec x))))))))
(defn integral [f h] (fn [x] (intergate f h (int (/ x h)))))
(def integral-cosx (integral (fn [x] (Math/cos x)) h)) (def integral-sinx (integral (fn [x] (Math/sin x)) h))
(defn -main [& args] (time (integral-cosx 0.5)) (time (integral-cosx 1)) (time (integral-cosx 2)) (time (integral-cosx 4)) (time (integral-cosx 5))
;;(t/is (= (format "%.2f" (Math/sin 1)) (format "%.2f" (integral-cosx 1))))
(time (integral-sinx 0.5)) (time (integral-sinx 1)) (time (integral-sinx 2)) (time (integral-sinx 4)) (time (integral-sinx 5))
;;(t/is (= (format "%.2f" (Math/sin 1)) (format "%.2f" (integral-cosx 1))))
)
- Assistant