On Dec 9, 8:48 pm, "Drew Olson" <[EMAIL PROTECTED]> wrote:
> On Tue, Dec 9, 2008 at 2:28 PM, Stephan Mühlstrasser <
>
> [EMAIL PROTECTED]> wrote:
>
> > On Dec 9, 12:34 am, "harrison clarke" <[EMAIL PROTECTED]> wrote:
> > > you're keeping the head of the sequence, and thus all the elements
> > between
> > > the head and nth.
>
> > > it's because you're using def, basically.
>
> Ok, I'm confused here. Why does wrapping his logic into a
> function alleviate this problem? Can you give a more detailed explanation,
> I'd like to understand what's going on here.
>
> - Drew
I hope that I understood it myself now, so I try to explain:
The wrapping into a function in the second approach is just for
convenience, and doesn't really matter. This is equivalent:
user=> (defn triangle-numbers [current cumulated]
(let [next (+ current cumulated)]
(lazy-cons next (triangle-numbers (inc current) next))))
#'user/triangle-numbers
user=> (nth (triangle-numbers 1 0) 10000000)
50000015000001
The problem with the first approach is that the head is assigned to a
variable, and this produces the caching effect. In the second approach
all items that are consumed from the sequence are discarded
afterwards, and thus no memory problem occurs.
Regards
Stephan
--~--~---------~--~----~------------~-------~--~----~
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
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
-~----------~----~----~----~------~----~------~--~---