I was looking at the source for the memoize
.
Coming from languages like C++/Python, this part hit me hard:
(let [mem (atom {})] (fn [& args] (if-let [e (find @mem args)] ...
I realize that memoize
returns a function, but for storing state, it uses a local "variable" mem
. But after memoize
returns the function, shouldn't that outer let vanish from scope. How can the function still refer to the mem
.
Why doesn't Clojure delete that outer variable, and how does it manage variable names. Like suppose, I make another memoized function, then memoize
uses another mem
. Doesn't that name clash with the earlier mem
?
P.S.: I was thinking that there must be something much be happening in there, that prevents that, so I wrote myself a easier version, that goes like http://ideone.com/VZLsJp , but that still works like the memoize
.