Re: A style question
- From: job-271842874@xxxxxxxxxxxxxx
- Date: Wed, 28 Feb 2007 02:33:08 -0500
Ken Tilton wrote:
Rob Warnock 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]
| | 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.
Yeah, I know I should just turn away, but I can't help myself :)
I'll have to file this away and come back to it when I've learned enough to understand it. Looks like read macros are involved, but I don't want to get ahead of myself...
.
kt
- Follow-Ups:
- Re: A style question
- From: Raffael Cavallaro
- Re: A style question
- From: Rob Warnock
- Re: A style question
- References:
- A style question
- From: job-271842874
- Re: A style question
- From: job-271842874
- Re: A style question
- From: Rob Warnock
- Re: A style question
- From: Ken Tilton
- A style question
- Prev by Date: Re: A style question
- Next by Date: Re: A style question
- Previous by thread: Re: A style question
- Next by thread: Re: A style question
- Index(es):
Relevant Pages
|