I've encountered an unexpected NullPointerException while implementing some beginning SICP code in Clojure. In particular, I want to implement the iterative square root procedure from section 1.1.7.
Here's the code:
(defn square [x] (* x x))
(defn abs [x]
(cond
(< x 0) (- x)
:else x))
(defn average [x y]
(/ (+ x y) 2))
(defn sqrt
([x] (sqrt 1.0 x))
([guess x]
(letfn [(good-enough? [guess]
(< (abs (- (square guess) x)) 0.001))
(improve [guess]
(average guess (/ x guess)))]
(if (good-enough? guess)
guess
(recur (improve guess) x)))))
This works fine for small enough values, e.g. (sqrt 16)
. I get a NullPointerException clojure.lang.Numbers.lt (Numbers.java:3693)
error for any inputs larger than approximately (square 2718)
.
Any ideas?
Update with the whole traceback (the previous one is all I get in the repl):
Exception in thread "main" java.lang.NullPointerException at clojure.lang.Numbers.lt(Numbers.java:3693) at sicp_in_clojure.chapter_one$sqrt$good_enough_QMARK___14.invoke(chapter_one.clj:40) at sicp_in_clojure.chapter_one$sqrt.invoke(chapter_one.clj:43) at sicp_in_clojure.chapter_one$sqrt.invoke(chapter_one.clj:37) at sicp_in_clojure.chapter_one$eval19.invoke(chapter_one.clj:48) at clojure.lang.Compiler.eval(Compiler.java:6465) at clojure.lang.Compiler.load(Compiler.java:6902) at clojure.lang.Compiler.loadFile(Compiler.java:6863) at clojure.main$load_script.invoke(main.clj:282) at clojure.main$script_opt.invoke(main.clj:342) at clojure.main$main.doInvoke(main.clj:426) at clojure.lang.RestFn.invoke(RestFn.java:408) at clojure.lang.Var.invoke(Var.java:401) at clojure.lang.AFn.applyToHelper(AFn.java:161) at clojure.lang.Var.applyTo(Var.java:518) at clojure.main.main(main.java:37)
Not sure if this is still relevant or not, but I thought it worth trying it out using the LightTable Playground app that lets you see how things are interpreted:
Have you tried with a recent build, say the new Clojure 1.4.0 release?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With