I got an error: Execution error (NullPointerException) at test0.core/trapezoidal-mem…
- User
I got an error: Execution error (NullPointerException) at test0.core/trapezoidal-mem (core.clj:28). Cannot invoke "Object.getClass()" because "x" is null Here's my code: (ns test0.core (:gen-class) ;;(require [clojure.test :as t]) )
(defn trapezoidal-integral [f a b h] (letfn [(recurse [h current] (if (>= current b) 0 (+ (f current) (recurse h (+ current h)))))] (* h (+ (/ (+ (f a) (f b)) 2) (recurse h (+ a h))))))
(defn busy-wait [n] (doall (map (fn [x] x) (range n))))
(defn function [x] (busy-wait 10000) (+ (* (* x x) x) (* 3 x) 159))
(def memoized-function (memoize function))
(defn trapezoidal-mem [memoized-f a b h] (let [cached-values (reduce (fn [memo x] (assoc memo x (memoized-f x))) {} (range a b h))] (letfn [(recurse [current] (if (>= current b) 0 (+ (cached-values current) (recurse (+ current h)))))] (* h (+ (/ (+ (cached-values a) (cached-values b)) 2) (recurse (+ a h)))))))
(defn -main [& args] (time (trapezoidal-integral function 5 10 0.01)) (time (trapezoidal-integral function 5 10 0.01)) (time (trapezoidal-mem memoized-function 5 10 0.01)) (time (trapezoidal-mem memoized-function 5 10 0.01))
(println (trapezoidal-mem memoized-function 5 10 0.01)) (time (trapezoidal-mem memoized-function 5 11 0.01)) )
- Assistant