Hi Guys.<div><br></div><div>Thanks for the education on for/fold and for*/fold.</div><div><br></div><div>Cristian, your solution is very elegant, thanks, but unfortunately it does generate all the numbers so is relatively slow and won&#39;t scale too well with a and b.  </div>
<div><br></div><div>-Joe<br><br><div class="gmail_quote">On Tue, Apr 17, 2012 at 11:01 AM, Cristian Esquivias <span dir="ltr">&lt;<a href="mailto:cristian.esquivias@gmail.com">cristian.esquivias@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I used Project Euler to try out new languages as well. Here was my<br>
attempt at Problem 29 for reference.<br>
<br>
(define (prob29 a b)<br>
  (set-count<br>
   (for*/fold<br>
       ([nums (set)])<br>
     ([i (in-range 2 (add1 a))]<br>
      [j (in-range 2 (add1 b))])<br>
     (values (set-add nums (expt i j))))))<br>
<span class="HOEnZb"><font color="#888888"><br>
<br>
- Cristian<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
On Tue, Apr 17, 2012 at 9:54 AM, Matthew Flatt &lt;<a href="mailto:mflatt@cs.utah.edu">mflatt@cs.utah.edu</a>&gt; wrote:<br>
&gt; Blindly refactoring the code, I&#39;d use `for/fold&#39; and add a `lst&#39;<br>
&gt; accumulator to `loop&#39;:<br>
&gt;<br>
&gt; (define (euler29c)<br>
&gt;  ; calculate 99^2 - duplicates<br>
&gt;  (- (sqr 99)<br>
&gt;     (for/sum ([d &#39;(2 3 5 6 7 10)])<br>
&gt;       (let loop ([lst &#39;()] [exp 1])<br>
&gt;         (if (&gt; (expt d exp) 100)<br>
&gt;             (- (length lst) (length (remove-duplicates lst)))<br>
&gt;             (loop (for/fold ([lst lst]) ([i (in-range 2 101)])<br>
&gt;                     (cons (* i exp) lst))<br>
&gt;                   (add1 exp)))))))<br>
&gt;<br>
&gt; At Tue, 17 Apr 2012 09:45:50 -0700, Joe Gilray wrote:<br>
&gt;&gt; Hi,<br>
&gt;&gt;<br>
&gt;&gt; To continue our conversation about creating idiomatic Racket code, here is<br>
&gt;&gt; some code I wrote last night to solve <a href="http://projecteuler.net" target="_blank">projecteuler.net</a> problem #29:<br>
&gt;&gt;<br>
&gt;&gt; (define (euler29a)<br>
&gt;&gt;   ; calculate 99^2 - duplicates<br>
&gt;&gt;   (- (sqr 99)<br>
&gt;&gt;      (for/sum ([d &#39;(2 3 5 6 7 10)])<br>
&gt;&gt;               (let ([lst &#39;()])<br>
&gt;&gt;                 (let loop ([exp 1])<br>
&gt;&gt;                   (if (&gt; (expt d exp) 100) (- (length lst) (length<br>
&gt;&gt; (remove-duplicates lst)))<br>
&gt;&gt;                       (begin<br>
&gt;&gt;                         (for ([i (in-range 2 101)]) (set! lst (cons (* i<br>
&gt;&gt; exp) lst)))<br>
&gt;&gt;                         (loop (add1 exp)))))))))<br>
&gt;&gt;<br>
&gt;&gt; It&#39;s fast (it avoids calculating a bunch of huge numbers), it gives the<br>
&gt;&gt; correct answer, so what&#39;s not to love?!<br>
&gt;&gt;<br>
&gt;&gt; Well, it starts off OK, but my eye stumbles over the following:<br>
&gt;&gt;<br>
&gt;&gt; 1) predeclaring lst and accessing it twice, related to each other<br>
&gt;&gt; 2) ugly single parameter named-let loop<br>
&gt;&gt; 3) ugly &quot;begin&quot; - not a big deal, but I just dislike when having to use<br>
&gt;&gt; begin<br>
&gt;&gt; 4) use of set!<br>
&gt;&gt;<br>
&gt;&gt; Here is a quick rewrite:<br>
&gt;&gt;<br>
&gt;&gt; (define (euler29b)<br>
&gt;&gt;   ; calculate 99^2 - duplicates<br>
&gt;&gt;   (- (sqr 99)<br>
&gt;&gt;      (for/sum ([d &#39;(2 3 5 6 7 10)])<br>
&gt;&gt;               (let ([lst &#39;()])<br>
&gt;&gt;                 (do ([exp 1 (add1 exp)])<br>
&gt;&gt;                   ((&gt; (expt d exp) 100) (- (length lst) (length<br>
&gt;&gt; (remove-duplicates lst))))<br>
&gt;&gt;                   (for ([i (in-range 2 101)]) (set! lst (cons (* i exp)<br>
&gt;&gt; lst))))))))<br>
&gt;&gt;<br>
&gt;&gt; It solves #2 and #3 above, but it is still fundamentally clunky.<br>
&gt;&gt;<br>
&gt;&gt; Can someone help and teach us all some tricks?  My instincts say it should<br>
&gt;&gt; be possible to use append-map, for/list and/or foldl to build a list of the<br>
&gt;&gt; duplicates then simply count them in the for/sum loop, but am still unable<br>
&gt;&gt; to do it.<br>
&gt;&gt;<br>
&gt;&gt; Thanks,<br>
&gt;&gt; -Joe<br>
&gt;&gt; ____________________<br>
&gt;&gt;   Racket Users list:<br>
&gt;&gt;   <a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
&gt; ____________________<br>
&gt;  Racket Users list:<br>
&gt;  <a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
</div></div></blockquote></div><br></div>