I’m trying to learn to program with the book Clojure for the Brave and True (CFTBAT). At the end of the crash course, the author makes us write a small programm to illustrate looping in Clojure. To explain the looping and recursing part of the program, here, the author writes a smaller example using loop
and then shows it’s possible to replace loop
with a normal function definition.
It’s this normal function definition example I can’t understand. Here is the code:
(defn recursive-printer
([]
(recursive-printer 0))
([iteration]
(println iteration)
(if (> iteration 3)
(println "Bye!")
(recursive-printer (inc iteration)))))
(recursive-printer)
I don’t understand the code because I can’t see where are the arguments of the function recursive-printer
. In Clojure, the arguments of a function are supposed to be in brackets and the body in parenthesis. So, in this example, the arguments would be an empty argument []
and iteration
. But then why are they put between parenthesis too?
And what is (recursive-printer 0)
Is it a function call, where the function calls itself?
If someone could explain me how this piece of code works, that would be much appreciated.
[]
-- with code to handle the case where it's called with no arguments -- and one[iteration]
, to handle when it's called with one argument. – Charles Duffyiteration
until it reaches 4? – guillaume8375