[racket] Making a Racket function "recallable"

From: Phil Bewig (pbewig at gmail.com)
Date: Wed Feb 15 14:58:55 EST 2012

You might be interested in SRFI-41 <http://srfi.schemers.org/srfi-41/>. One
of the examples is an infinite stream of fibonacci numbers.

On Wed, Feb 15, 2012 at 1:50 PM, Joe Gilray <jgilray at gmail.com> wrote:

> 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).
>
> -Joe
>
> #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 strm))))))
>
>
> 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?
>>
>
>
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20120215/0874e627/attachment-0001.html>

Posted on the users mailing list.