So far it seems as if everyone's is passing the test! Great one guys! The level of programming here is obviously "better" than some other places!
https://news.ycombinator.com/item?id=3354027Makes me weep for the "future" of programming! And probably understand why we get so many bugs in our programs.
Anyhoo ... my first "obvious" attempt (i.e. literally following the description of the question):
Obviously very inefficient as it does at least 2 (on average nearly 4) division operations (which is one of the slowest CPU instructions you can give). I think that's why that benchmark shows such good results even for the nth version: nth is reasonably fast of short lists (which this definitely is).
So omitting one division (I think most of you got this one):
(cond ((= (rem n
15) 0) "FizzBuzz") (n)))
Trying to reduce on the number of divisions and using assoc instead of nth ... ehhh ... don't truly know which "should" be better
(cond ((cdr (assoc (rem n
15) '
((0 .
"FizzBuzz") (3 .
"Fizz") (5 .
"Buzz") (6 .
"Fizz") (9 . "Fizz") (10 . "Buzz") (12 . "Fizz")))))
(n)))
And then rather using member instead
(defun IB:FizzBuzz2
(n
/ r
) ((member r '
(3 6 9 12)) "Fizz") (n)))
Of course the nth function would make it lots less circuitous. ... uh! ... scratch that my FizzBuzz3 is the exact same thing as ph:fb5 ... and I thought I got it so well!
I think none of mine is bringing anything "new" to the table. You guys already covered everything I had thought of myself, and then some!
Actually ... just though of another:
(defun IB:FizzBuzz4
(n
/ L
) n))
Sorry, it's probably been thought of by some of yourselves as well! Not that it's doing much different really - still performing 2 division operations and then all sorts of list manipulations, so speedwise I'd not hold my breath.
Anyhow, I've recently started looking into Haskell. Here's my first "very obvious" try (i.e. same as my FizzBuzz0 routine):
fizzBuzz n
| n `
mod`
15 == 0 = "FizzBuzz" | n `
mod`
5 == 0 = "Fizz" | n `
mod`
3 == 0 = "Buzz"
Well ... not "exactly" the same since Haskell is static typed it's a bit difficult making a list with a mix of ints and strings like the Lisp does. I'm probably just not knowledgeable enough yet to get to the point fo doing it "properly" idiomatically! Definitely not to the degree in rosetacode, actually it's "exactly" the same as the 1st one there
! I actually like that lazy infinite list approach ... wonder if it would be much faster since it never uses any divisions.
That drives me to wish we had lazy eval in AutoLisp ... another thing ADesk removed from the XLisp core they stole. I suppose one could simulate it by using a cached list, but it would be much less than optimal as it would need to grow at the end (not at the front, so either lots of reverses or lots of appends). Not to mention it would start degrading on larger numbers due to nth being a bit slow when the list is long.