Warning: extreme newbie question ahead.<div><br></div><div>I wrote the following fibonacci function:</div><div><br></div><div><div>; function that returns the next fibonacci number each time it is called</div><div>; invoke as (fib)</div>
<div>(define fib</div><div> (let ([n0 -1] [n1 1])</div><div> (lambda ()</div><div> (let ([next (+ n0 n1)])</div><div> (set! n0 n1)</div><div> (set! n1 next))</div><div> n1)))</div></div><div><br>
</div><div>This function works, but is not "recallable" (is there a better word?). In other words I cannot do the following:</div><div><br></div><div><div style>(define f1 fib)</div><div style>(define f2 fib)</div>
<div style>(f1) (f1) (f2) ...</div><div style><br></div><div style>and have f1 and f2 be separate fibonacci lists. Somehow n0 and n1 have to be instance-specific and right now they are not.</div></div><div style><br></div>
<div style>Because of this limitation the following useful looking function only works the first time it is called:</div><div style><br></div><div style><div><font color="#222222" face="arial, sans-serif">; function that returns a list of fibonacci numbers less than the passed argument</font></div>
<div><font color="#222222" face="arial, sans-serif">(define (fib-less-than-n n)</font></div><div><font color="#222222" face="arial, sans-serif"> (let ([fib-val (fib)])</font></div><div><font color="#222222" face="arial, sans-serif"> (if (>= fib-val n) '() (cons fib-val (fib-less-than-n n)))))</font></div>
<div><font color="#222222" face="arial, sans-serif"><br></font></div><div><font color="#222222" face="arial, sans-serif">Any help appreciated. Any tips about stupidities in the code above welcome too!</font></div><div><font color="#222222" face="arial, sans-serif"><br>
</font></div><div><font color="#222222" face="arial, sans-serif">Thanks,</font></div><div><font color="#222222" face="arial, sans-serif">-Joe</font></div></div>