From: job-271842874 on
job-271842874(a)craigslist.org 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]

I thought about retrofitting my Ruby version as an exercise, but alas,
Ruby doesn't allow shifting truth to the left :)

Forgive my ignorance, but is anything like the boolean bit shifting
technique used in the Python code above possible in Lisp? No big loss if
it isn't, just curious.

I suppose it's unreasonable to expect the Lisp version to be as concise
as the Python version - not only is this a toy example, but I think a
language with more syntax will be able to provide more brevity in
certain situations. That's a tradeoff I'm willing to accept given the
benefits of a syntax that's more readily parsed and manipulated.

Brian

From: Ken Tilton on


job-271842874(a)craigslist.org wrote:
> job-271842874(a)craigslist.org 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
From: Rob Warnock on
<job-271842874(a)craigslist.org> 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]
|
| I thought about retrofitting my Ruby version as an exercise, but alas,
| Ruby doesn't allow shifting truth to the left :)
|
| Forgive my ignorance, but is anything like the boolean bit shifting
| technique used in the Python code above possible in Lisp? No big loss if
| it isn't, just curious.
+---------------

Well, sort of... ;-} ;-}

This one is both efficient -- *no* MOD calls at all! --
*and* so ugly only a parent could love it: ;-} ;-}

(defun fizz-buzz (n)
(loop for i from 1 to n
and three-p in '#3=(nil nil t . #3#)
and five-p in '#5=(nil nil nil nil t . #5#)
do (format t "~a~%" (cond
((and three-p five-p) "FizzBuzz")
(three-p "Fizz")
(five-p "Buzz")
(t i)))))


-Rob

-----
Rob Warnock <rpw3(a)rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607

From: Ken Tilton on


Rob Warnock wrote:
> <job-271842874(a)craigslist.org> 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]
> |
> | I thought about retrofitting my Ruby version as an exercise, but alas,
> | Ruby doesn't allow shifting truth to the left :)
> |
> | Forgive my ignorance, but is anything like the boolean bit shifting
> | technique used in the Python code above possible in Lisp? No big loss if
> | it isn't, just curious.
> +---------------
>
> Well, sort of... ;-} ;-}
>
> This one is both efficient -- *no* MOD calls at all! --
> *and* so ugly only a parent could love it: ;-} ;-}
>
> (defun fizz-buzz (n)
> (loop for i from 1 to n
> and three-p in '#3=(nil nil t . #3#)
> and five-p in '#5=(nil nil nil nil t . #5#)
> do (format t "~a~%" (cond
> ((and three-p five-p) "FizzBuzz")
> (three-p "Fizz")
> (five-p "Buzz")
> (t i)))))

Yer a sick puppy, Rob, but I like it.

kt
From: Brian Adkins on
Ken Tilton wrote:
> job-271842874(a)craigslist.org wrote:
>> job-271842874(a)craigslist.org 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)))))

(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
>