Hi Mike,
thank you for your reply!
On Sep 1, 3:15 pm, "Michael Reid" <[EMAIL PROTECTED]> wrote:
> (defn fib [n]
> (let [fibs2 (fn fibs2 [] (lazy-cat '(0 1) (map + (fibs2) (drop 1
> (fibs2)))))]
> (nth (fibs2) n)))
Dodgy. ;-) I wasn't aware of named anonymous fns – nice.
But i suspect disguising lazy seqs as fns prevents memoization from
happening, which is the benefit of using lazy seqs over naive
recursion. So on each (fibs2) call, a completely new fibs sequence is
built, without reusing values computed earlier, it seems:
With
(def fibs (lazy-cat '(0 1) (map + fibs (drop 1 fibs))))
(defn fib [n]
(let [fibs2 (fn fibs2 [] (lazy-cat '(0 1) (map + (fibs2) (drop 1
(fibs2)))))]
(nth (fibs2) n)))
freshly defined, (nth fibs 10000) finishes in a couple of seconds,
while (fib 10000) might run for ages …
Any ideas very much appreciated!
Thanks again,
kind regards,
achim
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---