<div dir="ltr">On Tue, Apr 9, 2013 at 2:51 PM, Laurent <span dir="ltr"><<a href="mailto:laurent.orseau@gmail.com" target="_blank">laurent.orseau@gmail.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="im"><div class="gmail_extra">On Tue, Apr 9, 2013 at 2:42 PM, Matthew Flatt <span dir="ltr"><<a href="mailto:mflatt@cs.utah.edu" target="_blank">mflatt@cs.utah.edu</a>></span> wrote:<br>
<div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"> (for/product ([p (in-range (+ m 1) (+ n 1))]) p)</blockquote>
</div></div></div><div class="gmail_extra"><br><br></div><div class="gmail_extra">But I don't fully understand why a simple (for/product ([p (in-range 1 (add1 n))]) p) is as fast as the iota variants.<br></div></div>
</blockquote><div><br></div><div style>I don't understand either.</div><div style><br></div><div style>FWIW here's the kind of multiplications performed by the doctored version attached below:</div><div style><br>
</div><div style><div>> (fact 1000000)</div><div>#hash(((fíx fíx fíx) . 48577) ((big big) . 209717) ((fíx big) . 46338) ((fíx fíx) . 646789))</div><div><br></div><div style>while the for version should be almost all big x big.</div>
<div><br></div><div>====</div><div><div><br></div><div>(define x*-counter 'not-initialized)</div><div><br></div><div>(define (x* . args)</div><div> (hash-update! x*-counter (map (λ (x)</div><div> (if (fixnum? x)</div>
<div> 'fíx</div><div> 'big))</div><div> args)</div><div> add1</div><div> 0)</div><div> (apply * args))</div>
<div><br></div><div>;; from included; to excluded.</div><div>(define (mult from to)</div><div> (case (- to from)</div><div> ((0) 1)</div><div> ((1) from)</div><div> ((2) (x* from (+ from 1)))</div><div> ((3) (x* from (+ from 1) (+ from 2)))</div>
<div> ;; ...</div><div> (else</div><div> (let ((middle (quotient (+ from to) 2)))</div><div> (x* (mult from middle) (mult middle to))))))</div><div><br></div><div>(define (fact n)</div><div> (set! x*-counter (make-hash))</div>
<div> (define f (if (zero? n)</div><div> 1</div><div> (mult 2 (add1 n))))</div><div> (values ;f</div><div> x*-counter))</div></div><div><br></div></div><div style><br></div><div style><br></div>
<div style><br></div></div></div></div>