Reputation: 2316
I am new to Clojure and am trying to understand why the two functions below are different.
First
(defn rp [i]
((println i)
(if (> i 3)
(println "bye")
(rp (inc i)))))
Second
(defn rp
([i] (println i)
(if (> i 3)
(println "bye")
(rp (inc i)))))
When I call them with (rp 0)
, the first prints
0
1
2
3
4
bye
CompilerException java.lang.NullPointerException, compiling:(/Users/...)
and the second prints
0
1
2
3
4
bye
=> nil
Why are they different? Why the first function triggers a NullPointerException
?
Upvotes: 0
Views: 74
Reputation: 37073
Your first example has parens around the body and calls the result of (println i)
(which is nil
) as a function. E.g. ((println :a) :b)
throws. Most likely related to your experiments with the different arities in your second example.
Upvotes: 2