I just started playing with Clojure and I wrote a little script to help me understand some of the features. It starts as follows:
(def *exprs-to-test* [
"(filter #(< % 3) '(1 2 3 4 3 2 1))"
"(remove #(< % 3) '(1 2 3 4 3 2 1))"
"(distinct '(1 2 3 4 3 2 1))"
])
Then it goes through *exprs-to-test*, evaluates them all and prints the output as follows:
(doseq [exstr *exprs-to-test*]
(do
(println "===" (first (read-string exstr)) "=========================")
(println "Code: " exstr)
(println "Eval: " (eval (read-string exstr)))
)
)
The above code is working fine. However (read-string exstr), it repeats, so I tried to use letit to eliminate the repetition like this:
(doseq [exstr *exprs-to-test*]
(let [ex (read-string exstr)] (
(do
(println "===" (first ex) "=========================")
(println "Code: " exstr)
(println "Eval: " (eval ex))
)
))
)
But this works once for the first item in *exprs-to-test*, then crashes with NullPointerException. Why letdoes adding fail?
source
share