OK, I read some more and played around and got the behavior I wanted as follows:<div><br></div><div><div>(define primes-from-to</div><div> (let ([lastend 0] [storedlst '()])</div><div> (lambda (start end)</div><div>
(cond [(= lastend 0) (set! storedlst (primes-to end))]</div><div> [(= lastend start) (set! storedlst (sieve (append storedlst (interval-list start end))))]</div><div> [(< lastend end) (primes-from-to lastend end)])</div>
<div> </div><div> ; storedlst now has all the primes needed</div><div> (set! lastend end) </div><div> (filter (lambda (v) (if (and (>= v start) (<= v end)) #t #f)) storedlst)</div><div> )))</div><div>
<br></div><div>Now lastend and storedlst are preserved between calls and I get the speed up I expected:</div><div><br></div><div><div>> (time (primes-from-to 60000 60050))</div><div>cpu time: 6630 real time: 6694 gc time: 967</div>
<div>'(60013 60017 60029 60037 60041)</div><div>> (time (primes-from-to 60000 60030))</div><div>cpu time: 0 real time: 1 gc time: 0</div><div>'(60013 60017 60029)</div></div><div><br></div><div>I'd like to understand more about this, if anyone would care to enlighten me, I'd love to see more examples than I can find in the guide and reference.</div>
<div><br></div><div>Thanks,</div><div>-joe</div><br><div class="gmail_quote">On Fri, Feb 17, 2012 at 9:05 PM, Joe Gilray <span dir="ltr"><<a href="mailto:jgilray@gmail.com">jgilray@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I've seen Eli's example of static variables on Stackoverflow, but I still need help.<div><br></div><div>I've created a working sieve and now I want to wrap it in a manner that save primes to be used later.</div>
<div><br></div><div>This is what I tried:</div><div><br></div><div><div>; wrapper function for sieve</div><div>(define (primes-from-to start end)</div><div> (let ([lastend 0] [storedlst '()])</div><div> (lambda ()</div>
<div> (cond [(= lastend 0) (set! storedlst (primes-to end))]</div><div> [(= lastend start) (set! storedlst (sieve (append storedlst (interval-list start end))))]</div><div> [(< lastend end) (primes-from-to lastend end)])</div>
<div> </div><div> ; storedlst now has all the primes needed</div><div> (set! lastend end) </div><div> (filter (lambda (v) (if (and (>= v start) (<= v end)) #t #f)) storedlst)</div><div> )))</div></div>
<div><br></div><div>It works, but I can't get any speed advantages as I simply don't know how to syntactically vary start and end and save lastend and storedlst.</div><div><br></div><div>(define a (prime-from-to 100 200)) is not useful when I want to later call (a 50 70).</div>
<div><br></div><div>I read the manual about all the define* functions but really nothing seems to fit, what am I missing?</div><div><br></div><div>Thanks,</div><div>-joe</div>
</blockquote></div><br></div>