Hi Tim,<div><br></div><div>Thanks for sharing your code.</div><div><br></div><div>Quick, newby question: why do you use "and" instead of "begin" in your progress function? I've seen others do this as well (of course, Eli's "(and...)" idiom above is a whole different animal... and very cool.</div>
<div><br></div><div>Thanks,</div><div>-Joe</div><div><br><div class="gmail_quote">On Wed, Mar 21, 2012 at 10:01 AM, Tim Brown <span dir="ltr"><<a href="mailto:tim.brown@cityc.co.uk">tim.brown@cityc.co.uk</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Rodolfo,<br>
<br>
I started Project Euler with nested (do ...) loops.<br>
Which are beyond ugly. It was truly revolutionary<br>
finding the for/... forms.<br>
<br>
Get a hang of them!<br>
<br>
Also, you might like to use:<br>
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<u></u>;;;;;;;;;;;;;;;;;;;<br>
(define last-progress-ms #f)<br>
;;; it's global, it's expeident, but it has worked for me so far!<br>
(define (progress i period (max-i #f))<br>
(when (= (modulo i period) 0)<br>
(let* ((now-ms (current-milliseconds))<br>
(delta-raw (and last-progress-ms (- now-ms last-progress-ms)))<br>
(delta-s (and delta-raw (quotient delta-raw 1000)))<br>
(delta-ms (and delta-raw (remainder delta-raw 1000))))<br>
(set! last-progress-ms now-ms)<br>
(if<br>
max-i<br>
(and<br>
(printf "~a+~a:~a.~a|" i (- max-i i) delta-s delta-ms)<br>
(flush-output))<br>
(and (printf "~a:~a.~a|" (/ i period) delta-s delta-ms)<br>
(flush-output))))))<br>
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<u></u>;;;;;;;;;;;;;;;;;;;<br>
<br>
It's useful in loops like:<br>
<br>
(let ((stupid-number-youll-find-so-<u></u>often-in-PE(expt 10 1034)))<br>
(for*/first<br>
(<br>
[k (in-range 0 stupid-number-youll-find-so-<u></u>often-in-PE)]<br>
#:when (progress (- stupid-number-youll-find-so-<u></u>often-in-PE k) 1000000)<br>
[i (in-range 0 10)]<br>
[j (in-range 0 10)]<br>
...)))<br>
<br>
You'll know when you'll need it!<br>
#:when (printf ...)<br>
will become your friend!<br>
<br>
Tim<div class="im"><br>
<br>
Eli Barzilay wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
20 minutes ago, Rodolfo Carvalho wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
(define (pythagorean-triple/alt n)<br>
(for*/first ([a (in-range 1 n)]<br>
[b (in-range a n)]<br>
[c (in-value (- n a b))]<br>
#:when (right-triangle? a b c))<br>
(list a b c)))<br>
</blockquote>
<br>
You can also use `for*/or':<br>
<br>
(define (pythagorean-triple/alt n)<br>
(for*/or ([a (in-range 1 n)]<br>
[b (in-range a n)])<br>
(define c (- n a b))<br>
(and (right-triangle? a b c) (list a b c))))<br>
<br>
</blockquote>
<br>
<br>
-- <br></div>
Tim Brown <<a href="mailto:tim.brown@cityc.co.uk" target="_blank">tim.brown@cityc.co.uk</a>> | City Computing Limited |<br>
T: <a href="tel:%2B44%2020%208770%202110" value="+442087702110" target="_blank">+44 20 8770 2110</a> | City House, Sutton Park Road |<br>
F: <a href="tel:%2B44%2020%208770%202130" value="+442087702130" target="_blank">+44 20 8770 2130</a> | Sutton, Surrey, SM1 2AE, GB |<br>
------------------------------<u></u>------------------------------<u></u>-----------|<br>
BEAUTY: What's in your eye when you have a bee in your hand |<br>
------------------------------<u></u>------------------------------<u></u>-----------'<br>
City Computing Limited registered in London No. 1767817.<br>
Registered Office: City House, Sutton Park Road, Sutton, Surrey, SM1 2AE<br>
VAT number 372 8290 34.<div class="HOEnZb"><div class="h5"><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><br>
</div></div></blockquote></div><br></div>