I’m trying to read up a bit on Clojure, but I hit a brick wall with the following basic example:
(defn make-adder [x]
(let [y x]
(fn [z] (+ y z))))
(def add2 (make-adder 2))
(add2 4)
-> 6
What I don’t understand is how is add2 passing the number 4 to the make-adder function, and how does that function turn assigns that number to z.
Thanks in advance!
make-adderreturns a function that takes one parameter (z), the parameter passed in tomake-adderis used to assign a value to y.add2is set equal to the result of evaluatingmake-adderwith a parameter of 2. Soadd2is set equal to the function returned frommake-adder, which (since y has been assigned to the parameter frommake-adder) looks likeSo
(add2 4)calls this function which evaluates to 6. Does that help?