This function:
(defn make-adder [x]
(let [y x]
(fn [z] (+ y z))))
Itself returns a first-class function (a function that can be returned, passed around and assigned to a name just like any other value). Inside the let
, x
is bound to y
, and so the function above is equivalent to this function below:
(defn make-adder [x]
(fn [z] (+ x z)))
So that when make-adder
is called with the value 2
, it returns this first-class function:
(fn [z] (+ 2 z))
Remember, a first-class function can be assigned to a name, as in this line:
(def add2 (made-adder 2))
The line above is therefore equivalent to this code:
(def add2 (fn [z] (+ 2 z)))
And so is equivalent to this code also:
(defn add2 [z] (+ 2 z))
Which means that when add2
is called:
(add2 4)
The expression above evaluates to this:
(+ 2 4)
Which evaluates to 6
.