<div dir="ltr"><div>Hi Mark</div><div><br></div><div>Two ideas:</div><div><ol><li>add a bit of state with for/list to save a reference to the expensive computation</li><li>filter-map keeps the reference more localised</li>
</ol>E.g.<br></div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><font face="courier new, monospace">#lang racket</font></div><div><span style="font-family:'courier new',monospace">(require math/number-theory)</span><br>
</div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">(define (mersenne-primes n)</font></div><div><font face="courier new, monospace"> (let ([c 'dummy])</font></div>
<div><font face="courier new, monospace"> (for/list ([i n]</font></div><div><font face="courier new, monospace"> #:when (begin</font></div><div><font face="courier new, monospace"> (set! c (sub1 (expt 2 i)))</font></div>
<div><font face="courier new, monospace"> (prime? c)))</font></div><div><font face="courier new, monospace"> (list i c))))</font></div><div><br></div><div><font face="courier new, monospace">(define (mersenne-primes-2 n)</font></div>
<div><font face="courier new, monospace"> (filter-map (λ (i)</font></div><div><font face="courier new, monospace"> (let ([c (sub1 (expt 2 i))])</font></div><div><font face="courier new, monospace"> (and (prime? c) </font></div>
<div><font face="courier new, monospace"> (list i c))))</font></div><div><font face="courier new, monospace"> (range n)))</font></div><div><font face="courier new, monospace"><br></font></div>
<div><font face="courier new, monospace">(mersenne-primes 128)</font></div><div><font face="courier new, monospace">(mersenne-primes-2 128) </font></div></blockquote><div class="gmail_extra"><br></div><div class="gmail_extra">
Dan</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sun, Aug 17, 2014 at 7:03 PM, Mark Wallace <span dir="ltr"><<a href="mailto:lotabout@gmail.com" target="_blank">lotabout@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Consider the following pseudocode:<br>
<br>
(for/list ([i ...]<br>
#:when <very complecated expr E>)<br>
<reference to the above expr E>)<br>
<br>
Can I bind that expression E and reference in the body of "for/list"?<br>
<br>
If expression E takes lots of time to finish, we would not want to compute it again :).<br>
<br>
I understand that there are workarounds like first collect all items of expression E and then use 'filter' to get interested items. However it would be a waste of memory if the number of interested items is small.<br>
<br>
So, any ideas?<span class="HOEnZb"><font color="#888888"><br>
<br>
-- <br>
Best Regards,<br>
Mark Wallace.<br>
____________________<br>
Racket Users list:<br>
<a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/<u></u>users</a></font></span></blockquote></div><div dir="ltr"><div style="font-family:arial;font-size:small"></div></div>
</div></div>