[racket] static variables question

From: Neil Van Dyke (neil at neilvandyke.org)
Date: Sun Feb 19 05:02:09 EST 2012

Some random feedback, from a quick glance at the code (I didn't work 
through the algorithm)...

Do you mean to do "[candidate-primes (primes-from-to 2 1000)]" each time 
that "factor" is called?

2 and 1000 are magic numbers, and should at least be given names, for 
documentation purposes and so that the constants appear in only one place.

Also consider whether these magic numbers are arbitrary limits that are 
unnecessary, and then whether they should be arguments or irrelevant to 
the algorithm.

For more idiomatic Racket, try to get rid of the "set!" forms, such as 
by passing those values as part of a named-"let".

If "end" is always non-negative, is "(or (> end (integer-sqrt x)) (> (* 
1.25 end) (integer-sqrt x)))" redundant?

You are doing "(integer-sqrt x)" a few times, when "x" can't change in 
between.  You might want to have the code look like "(integer-sqrt x)" 
is computed only once in the block of code in which "x" can't change.  
Just good practice, IMHO; I don't promise that it makes a performance 

You have some tests in nested "if" forms that look redundant.  Perhaps 
those can be adjusted so that redundant tests don't happen, such as by 
getting rid of the "and" and shuffling around the "if" forms and their 
clauses.  (Sometimes this can be done easily; other times, it requires 
creating little helper procedures, and gets messy.)

Formatting-wise, you might consider generally putting newlines between 
each of the three parts of an "if" form.  It's easier to distinguish the 
parts at a glance, especially if the parts contain parens, and you can 
also sometimes better see symmetries/asymmetries between the branches.

Lots of people use "append" casually, but if you get to the point of 
fine-tuning this code or some other code, I usually try to build up 
lists with the code pattern "(cons NEW-ELEMENT EXISTING-LIST)".  
Sometimes this means doing a "reverse" after the list is complete.  
However, assuming that the rest of the algorithm is essentially the 
same, whether avoiding "append" is actually faster can come down to 
characteristics of the garbage collector (and both the sizes and the 
lifetimes of the lists can be relevant), so I think you'd have to 
evaluate performance empirically.

You can use "(zero? X)" instead of "(= 0 X)".  This is a minor point of 
personal style: I have a bit of folk wisdom that, if you're testing 
numeric equality with a constant in an algorithm, such as a number that 
changes in a loop, most often that constant should be 0, and using 
"zero?" acknowledges that.

Joe Gilray wrote at 02/19/2012 04:05 AM:
> Here's a slight reworking of the factor function.  I think it is 
> prettier and my in the spirit of Racket/Scheme.


Posted on the users mailing list.