[racket] static variables question

From: Joe Gilray (jgilray at gmail.com)
Date: Sat Feb 18 00:05:35 EST 2012

I've seen Eli's example of static variables on Stackoverflow, but I still
need help.

I've created a working sieve and now I want to wrap it in a manner that
save primes to be used later.

This is what I tried:

; wrapper function for sieve
(define (primes-from-to start end)
  (let ([lastend 0] [storedlst '()])
    (lambda ()
    (cond [(= lastend 0) (set! storedlst (primes-to end))]
          [(= lastend start) (set! storedlst (sieve (append storedlst
(interval-list start end))))]
          [(< lastend end) (primes-from-to lastend end)])

    ; storedlst now has all the primes needed
    (set! lastend end)
    (filter (lambda (v) (if (and (>= v start) (<= v end)) #t #f)) storedlst)

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.

(define a (prime-from-to 100 200)) is not useful when I want to later call
(a 50 70).

I read the manual about all the define* functions but really nothing seems
to fit, what am I missing?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20120217/465cd497/attachment.html>

Posted on the users mailing list.