6 ms·
My favourite FizzBuzz is in Haskell, I found it in this talk by Kevlin Henney[0], and looks like this: fizzes = cycle ["", "", "Fizz"] buzzes = cycle [
by D7x7w9pHnT 5y ago
My favourite FizzBuzz is in Haskell, I found it in this talk by Kevlin Henney[0], and looks like this:
fizzes = cycle ["", "", "Fizz"]
buzzes = cycle ["", "", "", "", "Buzzes"]
words = zipWith (++) fizzes buzzes
numbers = map show [1..]
fizzbuzz = zipWith max words numbers
Henney explains in detail in the video, but it makes use of lazy evaluation to create an infinite list of FizzBuzzes, and also uses no if statements. I find it intellectually exciting, but make no claims on readability
[0]https://youtu.be/LueeMTTDePg?t=2891 https://youtu.be/LueeMTTDePg?t=2891
- jjnoakes 5y ago"max" seems like an odd choice. Wouldn't that start to fail at large numbers? Edit: Nevermind, max is lexicographic, not based on string length. Brain fart.
- D7x7w9pHnT 5y agoYes, that part is hacky. It's easy to write a short function to do the same thing explicitly but then you lose the concise charm
- rmetzler 5y agoThanks for explaining it!
- robocat 5y agoI think 0 should be a FizzBuzz? Also I'm not sure how that solution would work for negative numbers... Then again what is the right answer for 7i + 24? Corner cases duck!
- ijlx 5y agoI've never seen fizzbuzz defined in such a way that zero or negative numbers are part of the problem. Typically it is a counting game, starting at 1 and going up to some arbitrary value.
- D7x7w9pHnT 5y agoFor 0 being a FizzBuzz you can `cons` a "FizzBuzz" to the start of the list fizzbuzz = "FizzBuzz" : zipWith... To make negative numbers work you'd need a new numbers definition numbers = map show [-1, -2..] Should yield all the negative integers eventually. I've never heard of FizzBuzz defined for complex/imaginary/2-d numbers. That's interesting to consider, I'll be thinking about this for a while
- carnitine 5y agoSeems pretty readable to me if you’re comfortable with Haskell.