Functions in Clojure can behave differently depending on the number of
arguments they receive (their "arity"). A function can be written:
(defn foo [x]
(str "foo" x))
But you can also write:
(defn foo
([] "empty foo")
([x] (str "foo" x))
This will do different things depending on the number of arguments:
(foo)
=> "empty foo"
(foo "bar")
=> "foobar"
- James
On 4 September 2016 at 21:54, Charlie <[email protected]> wrote:
> I'm going through the Do Things: A Clojure Crash Course, and the following
> example (in REPL) is presented:
>
> (defn recursive-printer
> ([]
> (recursive-printer 0))
> ([iteration]
> (println iteration)
> (if (> iteration 3)
> (println "Goodbye!")
> (recursive-printer (inc iteration)))))(recursive-printer); =>
> Iteration 0; => Iteration 1; => Iteration 2; => Iteration 3; => Iteration 4;
> => Goodbye!
>
>
> This works as expected, but I don't understand the syntax of the function
> definition. Specifically: (defn recursive-printer ([] (recursive-printer
> 0)). Isn't the parameter list supposed to be a vector [] not ([]...)? Also,
> I don't see how [iteration] is resolved on the next line.
>
> Thanx for any help!
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected]
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.