{:check ["true"], :rank ["defn" "fn" "anonymous" "references"]}
The most familiar (and common) way to define functions in Clojure is using
the (defn ...)
construct:
(defn <name> [ <param> ... ]
<body>)
(defn greeting [username]
(str "Hello, " username "."))
; Now we can use it.
(println (greeting "Albert Einstein"))
;
Clojure supports metadata such as docstring, associated with the function defined.
(defn <name>
<doc string>
[param ...]
<body>)
(defn greeting
"Generate a greeting message as a string
for the given username."
[username]
(str "Hello, " username "."))
; Now we can get some help on the function
(println (:doc (meta (var greeting))))
;
; We can still use the function as always.
(greeting "Albert Einstein")
(defn greeting
"Generates a greeting message for the given username.
- No paramete: default message.
- username: message for the user.
- title username: message will refer to the user by the title."
([] "Hi there.")
([username] (str "Hello, " username "."))
([title username]
(str "Hello, " title ". " username ".")))
(println (:doc (meta (var greeting))))
;
(greeting)
(greeting "Albert Einstein")
(greeting "Prof" "Albert Einstein")
Anonymous functions are functions that are created without given a name in the definition.
We can always bind functions to symbols.
(fn [param ...] body)
(fn [username]
(str "Hello, " username "."))
((fn [username] (str "Hello, " username ".")) "Albert Einstein")
(let [f (fn [username] (str "Hello, " username "."))]
(f "Albert Einstein"))
; Note the symbol is only valid in the scope of `(let ...)`
(f "Albert Einstein")
Clojure require so much of function construction that it has an even shorter hand to create anonymous functions.
#(body)
%
is the first argument.%1
, %2
, %3
etc.#(str "Hello, " % ".")
(#(str "Hello, " % ".") "Albert Einstein")
(let [f #(str "Hello, " % ".")]
(f "Albert Einstein"))