Re: Memoizing a recursive function?

2010-07-23 Thread Mike Meyer
[Context lost to top posting.] On Thu, 22 Jul 2010 15:56:32 -0700 (PDT) logan wrote: > 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 > (f

Re: Memoizing a recursive function?

2010-07-23 Thread Laurent PETIT
Yes, because the code I posted still makes me wonder what's happening ... 2010/7/23 Meikel Brandmeyer > Hi, > > On Jul 23, 3:02 am, logan wrote: > > > paul what version did you use? your version was the first one I tried, > > and on 1.2 at least it does not work. > > It works on 1.1. I guess th

Re: Memoizing a recursive function?

2010-07-23 Thread Meikel Brandmeyer
Hi, On Jul 23, 3:02 am, logan wrote: > paul what version did you use? your version was the first one I tried, > and on 1.2 at least it does not work. It works on 1.1. I guess there is some issue with the direct linking introduced in 1.2. Sincerely Meikel -- You received this message because

Re: Memoizing a recursive function?

2010-07-23 Thread logan
paul what version did you use? your version was the first one I tried, and on 1.2 at least it does not work. Jules version works, except lambda should be fn in clojure. On Jul 22, 4:51 pm, Paul Mooser wrote: > Why not simply do: > > (defn fib [n] >   (println "called with " n) >   (if (> n 2) >

Re: Memoizing a recursive function?

2010-07-23 Thread logan
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 instantane

Re: Memoizing a recursive function?

2010-07-23 Thread ajuc
On 23 Lip, 01:51, Paul Mooser wrote: > 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 t

Re: Memoizing a recursive function?

2010-07-22 Thread Laurent PETIT
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

Re: Memoizing a recursive function?

2010-07-22 Thread Paul Mooser
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

Re: Memoizing a recursive function?

2010-07-22 Thread Laurent PETIT
nevermind, the following code does not work. Jules' one is the right one 2010/7/22 Laurent PETIT > 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 close

Re: Memoizing a recursive function?

2010-07-22 Thread Jules
(def fib (memoize (lambda ...))) On Jul 22, 1:25 pm, Laurent PETIT wrote: > 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. Nobod

Re: Memoizing a recursive function?

2010-07-22 Thread Laurent PETIT
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, -- L

Re: Memoizing a recursive function?

2010-07-22 Thread Mike Meyer
On Wed, 21 Jul 2010 14:47:12 -0700 (PDT) logan 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.

Re: Memoizing a recursive function?

2010-07-22 Thread dennis
You should make a LazySeq to momoize intermediate result: (defn fib[n] (if (> n 2) (+ (fib (- n 2)) (fib (- n 1))) 1)) (def fib (memoize fib)) (def fib-seq (map fib (iterate inc 0))) then take the result by nth: user=> (nth fib-seq 45) 1134903170 user=> (nth fib-seq 46) 1836311903 user

Memoizing a recursive function?

2010-07-22 Thread logan
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? Using the default memoize does not work correctly. the reason is even though the first call to fib is memoized, the recursive ca