Here's a slight reworking of the factor function. I think it is prettier and my in the spirit of Racket/Scheme.<div><br></div><div><div>; function to create a list of prime factors of a number</div><div>; invoke as (factor n)</div>
<div>(define (factor n)</div><div> (let loop-factors ([facts '()] [x n] [start 2] [end 1000] [candidate-primes (primes-from-to 2 1000)])</div><div> (if (and (eq? candidate-primes empty) (>= end (integer-sqrt x)))</div>
<div> (if (= 1 x) facts (append facts (list x)))</div><div> (begin</div><div> (if (eq? candidate-primes empty)</div><div> (begin</div><div> ; attempt to pull in more primes in an efficient manner</div>
<div> (set! start end)</div><div> (set! end (* 2 end))</div><div> (if (or (> end (integer-sqrt x)) (> (* 1.25 end) (integer-sqrt x))) (set! end (integer-sqrt x)) #f)</div>
<div> (set! candidate-primes (primes-from-to start end))</div><div> (loop-factors facts x start end candidate-primes))</div><div> (let ([candidate (first candidate-primes)]) </div>
<div> (if (= 0 (remainder x candidate))</div><div> (begin</div><div> (set! facts (append facts (list candidate)))</div><div> (loop-factors facts (quotient x candidate) start end candidate-primes))</div>
<div> (loop-factors facts x start end (rest candidate-primes)))))))))</div><br><div class="gmail_quote">On Sat, Feb 18, 2012 at 6:41 PM, Joe Gilray <span dir="ltr"><<a href="mailto:jgilray@gmail.com">jgilray@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Stephen, Gary, et al,<div><br></div><div>Thanks for the suggestions. Below is the code I've been playing with today. I'd love any and all suggestions. I've been coding in Racket for about a week so I'm sure there is a lot a grist for the mill below!</div>
<div><br></div><div>-Joe</div><div><br></div><div><div>; function that returns a list of all integers between the two arguments inclusive</div><div>(define (interval-list m n)</div><div> (if (> m n)</div><div> '()</div>
<div> (cons m (interval-list (+ 1 m) n))))</div><div><br></div><div>; sieve of erostosthenes </div><div>(define (sieve lst)</div><div> (define (remove-multiples n lst)</div><div> (if (null? lst) '()</div><div>
(if (= (modulo (car lst) n) 0)</div><div> (remove-multiples n (cdr lst))</div><div> (cons (car lst)</div><div> (remove-multiples n (cdr lst))))))</div><div> (if (null? lst) '() (cons (car lst) (sieve (remove-multiples (car lst) (cdr lst))))))</div>
<div><br></div><div>; wrapper function for sieve, saves a list of primes for future calls</div><div class="im"><div>(define primes-from-to</div><div> (let ([lastend 0] [storedlst '()])</div></div><div> (lambda (start end)</div>
<div> (cond [(<= lastend start) (set! storedlst (sieve (append storedlst (interval-list start end))))]</div><div class="im">
<div> [(< lastend end) (primes-from-to lastend end)])</div><div> </div><div> ; storedlst now has all the primes needed</div></div><div> (if (< lastend end) (set! lastend end) #f)</div><div>
(filter (lambda (v) (and (<= start v) (<= v end))) storedlst))))</div>
<div><br></div><div>; function to create a list of prime factors of a number</div><div>; invoke as (factor n)</div><div>(define (factor n)</div><div> (let loop-factors ([facts '()] [x n] [start 2] [end 1000] [candidate-primes (primes-from-to 2 1000)])</div>
<div> (if (and (eq? candidate-primes empty) (>= end (integer-sqrt x)))</div><div> (if (= 1 x) facts (append facts (list x)))</div><div> (begin</div><div> (if (eq? candidate-primes empty)</div>
<div> (begin</div><div> ; attempt to pull in more primes in an efficient manner</div><div> (set! start end)</div><div> (set! end (* 2 end))</div><div> (if (or (> end (integer-sqrt x)) (> (* 1.25 end) (integer-sqrt x))) (set! end (integer-sqrt x)) #f)</div>
<div> (set! candidate-primes (primes-from-to start end))) #f)</div><div> (if (eq? candidate-primes empty)</div><div> (if (= 1 x) facts (append facts (list x)))</div><div> (let ([candidate (first candidate-primes)]) </div>
<div> (if (= 0 (remainder x candidate))</div><div> (begin</div><div> (set! facts (append facts (list candidate)))</div><div> (loop-factors facts (quotient x candidate) start end candidate-primes))</div>
<div> (loop-factors facts x start end (rest candidate-primes)))))))))</div><div> </div></div><div class="HOEnZb"><div class="h5"><div><br><br><div class="gmail_quote">On Sat, Feb 18, 2012 at 6:02 PM, Stephen Bloch <span dir="ltr"><<a href="mailto:bloch@adelphi.edu" target="_blank">bloch@adelphi.edu</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><br>
On Feb 18, 2012, at 2:42 PM, Gary Baumgartner wrote:<br>
<br>
>> (filter (lambda (v) (if (and (>= v start) (<= v end)) #t #f))<br>
>> storedlst)<br>
>> )))<br>
> [...]<br>
><br>
> Consider just (lambda (v) (and (>= v start) (<= v end))) --- no 'if'.<br>
<br>
</div>I see a lot of my students doing this -- in whatever language -- because they think of Booleans as a way to decide which of two things to DO, rather than as legitimate values in their own right. In fact, the whole world of expressions is a bit of a foreign country -- a sort of "adjunct" to the more-legitimate world of statements.<br>
<br>
if (blah == true) {<br>
return true;<br>
}<br>
else {<br>
return false;<br>
}<br>
<br>
For those of us forced to teach in Java, CheckStyle has two modules, SimplifyBooleanExpression and SimplifyBooleanReturn, that catch things like this.<br>
<span><font color="#888888"><br>
<br>
Stephen Bloch<br>
<a href="mailto:sbloch@adelphi.edu" target="_blank">sbloch@adelphi.edu</a><br>
</font></span><div><div><br>
<br>
____________________<br>
Racket Users list:<br>
<a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>