I'm taking the beginner tutorial on the Clojure official site.
One exercise asks you to do the following:
7) Define a function
triplicatewhich takes another function and calls it three times, without any arguments.
Which I solved as follows:
(defn triplicate [f] ((f) (f) (f)))
The problem is that when used, at the very end a NullPointerException is thrown, for which I cannot identify the cause.
user=> (triplicate #(println "hello, world"))
hello, world
hello, world
hello, world
NullPointerException user/triplicate (NO_SOURCE_FILE:115)
The following is the output of pst, should it be useful:
user=> (pst)
NullPointerException
user/triplicate (NO_SOURCE_FILE:145)
user/triplicate (NO_SOURCE_FILE:145)
user/eval376 (NO_SOURCE_FILE:146)
user/eval376 (NO_SOURCE_FILE:146)
clojure.lang.Compiler.eval (Compiler.java:7062)
clojure.lang.Compiler.eval (Compiler.java:7025)
clojure.core/eval (core.clj:3206)
clojure.core/eval (core.clj:3202)
clojure.main/repl/read-eval-print--8572/fn--8575 (main.clj:243)
clojure.main/repl/read-eval-print--8572 (main.clj:243)
clojure.main/repl/fn--8581 (main.clj:261)
clojure.main/repl (main.clj:261)
nil
What could the cause be?
(defn triplicate [f] (f) (f) (f)), which then returns the last result. Or add anilat the end to even prevent that. Adefnhas an impliciydoaround the body.