I tried defn-memo .. it still does not appear to memoize the original fib call? I tried this using clojure 1.2 beta. Reading the code definition for defn-memo it seems like it should work, but calling (fib 41) still takes a long time after calling (fib 40), when it should basically be an instantaneous call if the memoization works correctly.
Jules code works, except of course in clojure there is no lambda, but fn. On Jul 22, 5:36 am, Laurent PETIT <laurent.pe...@gmail.com> wrote: > nevermind, the following code does not work. > > Jules' one is the right one > > 2010/7/22 Laurent PETIT <laurent.pe...@gmail.com> > > > > > Another solution, use the var, and then use memoize on your function as > > usual: > > > (defn fib[n] > > (if (> n 2) > > (+ (#'fib (- n 2)) (#'fib (- n 1)))) > > > (of course this was to answer closely to the question. Nobody would write > > fib like that in practice : good general question, bad example) > > > HTH, > > > -- > > Laurent > > > 2010/7/22 Mike Meyer <mwm-keyword-googlegroups.620...@mired.org> > > > On Wed, 21 Jul 2010 14:47:12 -0700 (PDT) > >> logan <duskli...@gmail.com> wrote: > > >> > Lets say I have the following function > > >> > (defn fib[n] > >> > (if (> n 2) > >> > (+ (fib (- n 2)) (fib (- n 1))) > >> > 1)) > > >> > and I want to memoize it, what is the right way to do it? > > >> Use defn-memo from clojure.contrib.def. > > >> <mike > >> -- > >> Mike Meyer <m...@mired.org> > >>http://www.mired.org/consulting.html > >> Independent Network/Unix/Perforce consultant, email for more information. > > >> O< ascii ribbon campaign - stop html mail -www.asciiribbon.org > > >> -- > >> 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<clojure%2bunsubscr...@googlegroups.com > >> > > >> 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 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