Question

If Clojure is lisp-1 and I have a function m, how is it possible to also have a var m that refers to a memoized version of the function m?

(defn m .....)

(def m (memoize m))

This is from the PragPub Clojure book by Halloway. I can't understand what's going on here, since functions and vars share the same namespace in Clojure as a lisp-1.

Was it helpful?

Solution

On p72 in Joy of Clojure:

Lisp-1 uses the same name resolution for function and value bindings... Because the same name-resolution scheme is used for functions and their arguments, there's a real possibility of shadowing existing functions with other locals or Vars.

So shadowing(or rebinding) vars is possible, like in your code:

(defn m ...)

(def m (memoize m))

(memoize m) returns the memoized version of function and this function is re-bound(override) to m. That's it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top