<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Sure, sounds good.<div><br></div><div>The (let loop ([vs '()]) ...) is a 'named let' (<a href="http://docs.racket-lang.org/guide/let.html?q=named%20let#(part._.Named_let)">http://docs.racket-lang.org/guide/let.html?q=named%20let#(part._.Named_let)</a>) which as the guide says is equivalent to (letrec ([loop (lambda (vs) ...)]) (loop '()) --- so 'loop' is a recursive function that we're using to iterate until (fib) is >= n, accumulating the values in the vs argument. If you're not familiar with letrec, another definition would be</div><div><br></div><div>(define (fib-less-than-n n)</div><div> (define fib (mk-fib))</div><div> (define (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> (loop '()))</div><div><div><div></div></div><blockquote type="cite"><div><div><div class="gmail_quote"><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; "><div style="word-wrap: break-word; "><div><div><div></div></div></div></div></blockquote></div></div></div></blockquote></div><div><div><div><div class="gmail_quote"></div></div></div><blockquote type="cite"><div><div><div class="gmail_quote"><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; "><div style="word-wrap: break-word; "><div><div><div></div></div></div></div></blockquote></div></div></div></blockquote></div><div><br></div><div>Thinking of loops as just a form of recursion can be a little strange at first. There's more information in the Racket guide here- <a href="http://docs.racket-lang.org/guide/Lists__Iteration__and_Recursion.html">http://docs.racket-lang.org/guide/Lists__Iteration__and_Recursion.html</a> or another page to check out might be <a href="http://mitpress.mit.edu/sicp/full-text/sicp/book/node15.html">http://mitpress.mit.edu/sicp/full-text/sicp/book/node15.html</a>. </div><div><br></div><div>Hope this helps,</div><div><br></div><div>- Erik</div><div><div><br></div><div><br><div><div>On Feb 14, 2012, at 1:00 AM, Joe Gilray wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">Thanks Erik. Very nice! Although I miss the recursion.<div><div><br></div><div>please help me understand how this works, especially (let loop ([vs '()]). What exactly is going on there?</div><div><br></div><div>Thanks again,</div>
<div>-joe<br><br><div class="gmail_quote">On Mon, Feb 13, 2012 at 9:51 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; ">
<div style="word-wrap:break-word">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 class="im"><div> (if (>= fib-val n)</div></div><div> (reverse vs)</div><div> (loop (cons fib-val vs))))))</div></div>
</div></div><span class="HOEnZb"><font color="#888888"><div><br></div><div>- Erik</div></font></span><div><div class="h5"><div><br><div><div>On Feb 13, 2012, at 10:41 PM, Joe Gilray wrote:</div><br><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" target="_blank">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">
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> (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><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" target="_blank">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></div></div></div></blockquote></div><br></div></div>
</blockquote></div><br></div>
</div>
</body></html>