Code:
(def fib
(lazy-seq
(concat [0 1] (map + fib (rest fib)
(take 10 fib) ;; Bomb
Got the error message: _StackOverflowError clojure.lang.RT.more
(RT.java:589)_
And the following solutions works well:
(def fib
(concat [0 1] (lazy-seq (map + fib (rest fib)
quot; method.
@Paulo Sérgio Almeida 's solution just the thing i wanted.
BTW, I found the lazy way is more effective then TCO, the loop-recur I
mean, and memoization. Using build-in lazy funciton perform better.
On Saturday, April 13, 2013 12:52:28 PM UTC+8, Liao Pengyu wrote:
>
> Hi, the
> you
> > get a second function with an (empty!) internal memory. If you don't use
> the
> > first one more than once you don't see any benefit from its memory.
> >
> > So, try this:
> >
> > (let [f (memoize fib-nocur)]
> > (time (f 30))
Hi, there. I have a question about the memoization in clojure.
I compare two functions to test the performance improvement of memoization:
(defn fib [n]
(if (or (zero? n) (= n 1))
1
(+ (fib (dec n) ) (fib (- n 2)
(time (fib 30))
get the result:
"Elapsed time: 316.65 msec