Re: A style question





job-271842874@xxxxxxxxxxxxxx wrote:
job-271842874@xxxxxxxxxxxxxx wrote:

def fizz_buzz n
1.upto(n) do |i|
print "Fizz" if fizz = (i % 3) == 0
print "Buzz" if buzz = (i % 5) == 0
puts fizz || buzz ? "" : i
end
end

fizz_buzz 100

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

(fizz-buzz 100)


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.

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).

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

--
Well, I've wrestled with reality for 35 years, Doctor, and
I'm happy to state I finally won out over it.
-- Elwood P. Dowd

In this world, you must be oh so smart or oh so pleasant.
-- Elwood's Mom
.



Relevant Pages

  • Re: A style question
    ... You have missed that what I am driving at is good programming, you are caught up in cleverness, the road to hell in programming. ... 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". ... accidentally Fizz and Buzz together, then the strings Fizz and Buzz must ...
    (comp.lang.lisp)
  • Re: A style question
    ... (if fizz (format t "Fizz")) ... (if buzz (format t "Buzz")) ... but is anything like the boolean bit shifting technique used in the Python code above possible in Lisp? ...
    (comp.lang.lisp)
  • Re: A style question
    ... (format t "~%")))) ... (defun output-multiple(x n str) ...
    (comp.lang.lisp)
  • Re: A style question
    ... (format t "~%")))) ... (defun output-multiple(x n str) ...
    (comp.lang.lisp)
  • Re: A style question
    ... One example was a program to print the numbers 1 to 100 except that "Fizz" should be substituted for numbers divisible by 3, "Buzz" should be substituted for numbers divisible by 5, and "FizzBuzz" should be substituted for numbers divisible by both 3 and 5. ... So, having received my "ANSI Common Lisp" and "Practical Common Lisp" books two days ago and having a *couple hours* of Lisp under my belt, I cranked out a Lisp version. ... (if fizz (format t "Fizz")) ...
    (comp.lang.lisp)