Here is what I get: (and BTW, my original version with #' seems to work, I'm a little bit puzzled ...)
user=> (defn fib [n] (println "called with " n) (if (> n 2) (+ (fib (- n 2)) (fib (- n 1))) 1)) (def fib (memoize fib)) #'user/fib user=> user=> #'user/fib user=> (fib 6) called with 6 called with 4 called with 2 called with 3 called with 1 called with 2 called with 5 called with 3 called with 1 called with 2 called with 4 called with 2 called with 3 called with 1 called with 2 8 user=> (defn fib [n] (println "called with " n) (if (> n 2) (+ (#'fib (- n 2)) (#'fib (- n 1))) 1)) (def fib (memoize fib)) #'user/fib user=> user=> #'user/fib user=> (fib 6) called with 6 called with 4 called with 2 called with 3 called with 1 called with 5 8 user=> 2010/7/23 Paul Mooser <[email protected]> > Why not simply do: > > (defn fib [n] > (println "called with " n) > (if (> n 2) > (+ (fib (- n 2)) (fib (- n 1))) > 1)) > > (def fib (memoize fib)) > > I inserted the println to verify when we were actually calling the > function, and I believe this works - fib only seems to get invoked a > single time for any given n. Is this solution incorrect in some way? > > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to [email protected] > Note that posts from new members are moderated - please be patient with > your first post. > To unsubscribe from this group, send email to > [email protected]<clojure%[email protected]> > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to [email protected] Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/clojure?hl=en
