<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Instead of calling fib-less-than-n recursively, you could add a loop inside, for example something like,<div><br></div><div><div><div><div>(define (fib-less-than-n n)</div><div> (define fib (mk-fib))</div><div> (let loop ([vs '()])</div><div> (let ([fib-val (fib)])</div><div> (if (>= fib-val n)</div><div> (reverse vs)</div><div> (loop (cons fib-val vs))))))</div></div></div></div><div><br></div><div>- Erik</div><div><br><div><div>On Feb 13, 2012, at 10:41 PM, Joe Gilray wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">Erik,<div><br></div><div>Intriguing idea and it works "flat", but if I write the following I end up with an infinite loop:</div><div><br></div><div><div>(define (fib-less-than-n n)</div><div> (let ([f1 (mk-fib)])</div>
<div> (let ([fib-val (f1)])</div><div> (if (>= fib-val n) '() (cons fib-val (fib-less-than-n n))))))</div><div><br></div>How would I use your idea in this case?</div><div><br></div><div>thanks!</div><div>-joe</div>
<div><br><div class="gmail_quote">On Mon, Feb 13, 2012 at 9:18 PM, Erik Silkensen <span dir="ltr"><<a href="mailto:eriksilkensen@gmail.com">eriksilkensen@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; position: static; z-index: auto; ">
Another option is you could turn change your fib function to be 'mk-fib' that returns a new instance of the fib function each time it's called:<br>
<br>
(define (mk-fib)<br>
<div class="im"> (let ([n0 -1] [n1 1])<br>
(lambda ()<br>
(let ([next (+ n0 n1)])<br>
(set! n0 n1)<br>
(set! n1 next))<br>
n1)))<br>
<br>
</div>- Erik<br>
<div><div class="h5"><br>
On Feb 13, 2012, at 10:10 PM, Danny Yoo wrote:<br>
<br>
> On Mon, Feb 13, 2012 at 11:52 PM, Joe Gilray <<a href="mailto:jgilray@gmail.com">jgilray@gmail.com</a>> wrote:<br>
>> Warning: extreme newbie question ahead.<br>
>><br>
>> I wrote the following fibonacci function:<br>
>><br>
>> ; function that returns the next fibonacci number each time it is called<br>
>> ; invoke as (fib)<br>
>> (define fib<br>
>> (let ([n0 -1] [n1 1])<br>
>> (lambda ()<br>
>> (let ([next (+ n0 n1)])<br>
>> (set! n0 n1)<br>
>> (set! n1 next))<br>
>> n1)))<br>
><br>
><br>
> One thing you can do is turn fib into a "sequence", and then from a<br>
> sequence into a stream that knows how to remember its previous values.<br>
><br>
> Here's what it might look like:<br>
><br>
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br>
> #lang racket<br>
> (require racket/sequence)<br>
><br>
> (define fib<br>
> (let ([n0 -1] [n1 1])<br>
> (lambda ()<br>
> (let ([next (+ n0 n1)])<br>
> (set! n0 n1)<br>
> (set! n1 next))<br>
> n1)))<br>
><br>
> (define fib-stream<br>
> ;; Here's a sequence of the function:<br>
> (let ([fib-sequence (in-producer fib 'donttellmecauseithurts)])<br>
><br>
> ;; Let's wrap it and turn it into a stream that remembers...<br>
> (sequence->stream fib-sequence)))<br>
><br>
><br>
> ;; Ok, we've got a stream. Let's look at its first few elements.<br>
> (define (peek-fibs n)<br>
> (for ([elt fib-stream]<br>
> [i (in-range n)])<br>
> (displayln elt)))<br>
><br>
> (peek-fibs 10)<br>
> (printf "-----\n")<br>
> (peek-fibs 20)<br>
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br>
><br>
</div></div>> ____________________<br>
> Racket Users list:<br>
> <a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
<br>
</blockquote></div><br></div>
</blockquote></div><br></div></body></html>