[racket] Making a Racket function "recallable"

From: Joe Gilray (jgilray at gmail.com)
Date: Wed Feb 15 14:50:01 EST 2012

Danny is correct... ouch (/.\ <- covering my head in shame).

Anyway to close the chapter on this, below is the final result using
Danny's stream suggestions/code.  I also have a non-streams version thanks
to Erik (and Stephen and Joshua).


#lang racket
(require racket/sequence)

; a sequence function that creates an "infinite" sequence then turns that
into a stream that remembers its previous values as we walk across it.
; invoke in a loop-expr (see below)
(define fib-stream
  (let ([fib-sequence (in-producer (fib) 'donttellmecauseithurts)])
    (sequence->stream fib-sequence)))

; function to generate a list of all values in a stream < n
(define (stream-less-than-n n strm)
  (let ([fib-val (stream-first strm)])
    (if (>= fib-val n) '() (cons fib-val (stream-less-than-n n (stream-rest

On Wed, Feb 15, 2012 at 9:19 AM, Danny Yoo <dyoo at cs.wpi.edu> wrote:

> On Wed, Feb 15, 2012 at 3:10 AM, Joe Gilray <jgilray at gmail.com> wrote:
> > The code that Danny wrote to create fib-stream works great with his
> > peek-fibs function, but I really don't understand stream-first.
> >
> > I wrote:
> >
> > ; function to use the fib-stream to generate a list of all fibs < n
> > (define (fib-stream-less-than-n n)
> >   (let ([fib-val (stream-first fib-stream)])
> >     (if (>= fib-val n) '() (cons fib-val (fib-less-than-n n)))))
>                                                    ^^^^^^^^^^^^^^^^
> Are you sure you mean to use fib-less-than-n here?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20120215/f1952f8d/attachment.html>

Posted on the users mailing list.