On Apr 18, 2010, at 9:21 AM, Yonov wrote: > Hi, > I am trying to implement memoize myself and have stucked on one place > for a lot of time. This is the code: > > (defn mymemoize [func] > (let [dict (ref (hash-map)) > inner #((let > [val (@dict %)] > (if (nil? val) > (do (dosync (alter dict conj {% (func %)})) (println (class > (@dict %))) (@dict %)) > val)))] > inner)) > > > (defn gg [x] > (if (= x 1) 1 2)) > (def ff (mymemoize gg)) > (ff 1) > > I am printing only for debuging purposes. Actually everything in the > do-block is OK, but there is no return value. Why?
The body of your anonymous function literal (#()) has an extra level of parentheses. Your code is coming up with the correct answer (the Integer 1) and then trying to execute it with no arguments. That's what this exception: java.lang.ClassCastException: java.lang.Integer cannot be cast to clojure.lang.IFn (NO_SOURCE_FILE:0) is trying to convey: This works: (defn mymemoize [func] (let [dict (ref (hash-map)) inner #(let [val (@dict %)] (if (nil? val) (do (dosync (alter dict conj {% (func %)})) (println (class (@dict %))) (@dict %)) val))] inner)) --Steve -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en