Re: A style question
- From: job-271842874@xxxxxxxxxxxxxx
- Date: Wed, 28 Feb 2007 01:09:47 -0500
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]
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
.
- Follow-Ups:
- Re: A style question
- From: André Thieme
- Re: A style question
- From: Joel Wilsson
- Re: A style question
- From: Rob Warnock
- Re: A style question
- From: Ken Tilton
- Re: A style question
- References:
- A style question
- From: job-271842874
- A style question
- Prev by Date: Re: A style question
- Next by Date: Re: sexp-aware theory of patches and "meta common lisp"
- Previous by thread: Re: A style question
- Next by thread: Re: A style question
- Index(es):
Relevant Pages
|