Re: A style question



In article <p6aFh.35598$p9.19111@xxxxxxxxxxxxxxxxxxxxxx>,
Brian Adkins <brian@xxxxxxxxx> wrote:

Ken Tilton wrote:
job-271842874@xxxxxxxxxxxxxx wrote:
job-271842874@xxxxxxxxxxxxxx wrote:
Another friend of mine commenting on the same FizzBuzz thread supplied
the following Python code. It certainly is concise:

for i in xrange(1,101):
print(str(i), "Fizz", "Buzz", "FizzBuzz")[(i%3==0)|(i%5==0)<<1]

But this is a programming disaster, stupid pet trick atop stupid pet
trick. It collapses in a heap the minute anything changes. It builds
into itself all sorts of things that just happen to be true. You have
missed that what I am driving at is good programming, you are caught up
in cleverness, the road to hell in programming.

Agreed. Don't read too much into "It certainly is concise." My statement
was intentionally terse.

In the context of the "FizzBuzz" toy example, it can be fun to look at
some clever code, but don't infer that I'm "caught up in cleverness". In
fact, I've counseled countless coders to comprehend the concealed costs
of clever code. *ducks*

Let me help you. If, as it seems, the spec is that FizzBuzz is not
accidentally Fizz and Buzz together, then the strings Fizz and Buzz must
appear only once in the program, as must the tests (mod x 3) and (mod x 5).

I feel the same way; I guess my original Lisp hack wasn't so terrible -
a couple 'o mods and a couple 'o strings. Incorporating some of the
feedback from the group gives the following:

(defun fizz-buzz (n)
(do ((i 1 (+ i 1))) ((> i n))
(let
((fizz (zerop (mod i 3)))
(buzz (zerop (mod i 5))))
(when fizz (princ "Fizz"))
(when buzz (princ "Buzz"))
(format t "~A~%" (if (or fizz buzz) "" i)))))

In DO you can have more than one clauses for iteration.
So you could get rid of the extra LET.


(fizz-buzz 100)

Now on to chapter 3 "Lists" :)

The punch line is that no good programmer could write anything in five
minutes, unless the instructions included: "Just frickin make these
results appear from this input."

kt

.