Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Infinite fibonacci sequence on wiki
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Graham Fawcett  
View profile  
 More options Jun 16 2008, 2:10 pm
From: Graham Fawcett <graham.fawc...@gmail.com>
Date: Mon, 16 Jun 2008 11:10:39 -0700 (PDT)
Local: Mon, Jun 16 2008 2:10 pm
Subject: Re: Infinite fibonacci sequence on wiki
On Jun 16, 1:07 pm, Christophe Grand <christo...@cgrand.net> wrote:

> Graham Fawcett a écrit :> Yet it leaks memory: calling (nth fib-seq N) for a large N results in
> > an OutOfMemoryError.

> What you get is a java.lang.OutOfMemoryError: Java heap space and not a
> stack overflow: you get out of memory because:
> - the sequence is memoized
> - the sequence is rooted (for the GC) and thus can't be reclaimed.

> > I know that the JVM does not offer tail-call optimization, but is
> > there a way to rewrite the example (perhaps with (recur ...)) so that
> > (nth fib-seq N) runs in constant space?

> Turning the seq into a fn will allow it to be reclaimed and thus the
> computation will run in constant space.

> (defn fib-seq[]

>   (concat
>    [0 1]
>    ((fn rfib [a b]
>         (lazy-cons (+ a b) (rfib b (+ a b)))) 0 1)))

> (let [r (nth (fib-seq) 100000)] r)

Hm, sadly it does not work: I still run out of heap space, running
this version of the code.

Graham


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.