I need to optimaze my function (create a new one) with memoize. How should I do it in a…
- User
I need to optimaze my function (create a new one) with memoize. How should I do it in a rigth way? my code: (ns test0.core (:gen-class) (:use clojure.test))
(defn f-linear [x] x) ; f(x) = x (defn f-square [x] (Thread/sleep 10) (* x x)) ; f(x) = x2 (defn f-polinom [x] (+ (* 2 x x) (* 3 x) 1)) ; f(x) = 2 * x2 + 3 * x + 1 (defn f-sin [x] (Math/sin x)) ; f(x) = sin(x) (defn f-exponential [x] (Math/exp x)) ; f(x) = e^x
(defn trapezoid [f a b] ; intergrala,b = (f(a) + f(b)) * h / 2 ; h = b - a
(* (* (+ (f a) (f b)) (- b a)) 0.5))
(defn integral [f a b h]
(if (< a b) (+ (trapezoid f a (+ a h)) (integral f (+ a h) b h)) ; _ _ _ 0))
; Use the name integral-mem
(defn -main [& args])
- Assistant
To optimize your
integralfunction using memoization in Clojure, you can wrap your function with a memoization utility. Clojure provides a built-inmemoizefunction that creates a memoized version of a given function.