towards a more elegant fizzbuzz
This morning I saw this tweet:
blogged: Clojure Kata #1 � Fizz Buzz https://t.co/kdZvqjzkRa
— JanVanRyswyck (@JanVanRyswyck) 19 december 2013
As Jan is learning Clojure, he was exploring the fizzbuzz code kata.
I have seen this kata being performed in a lot of languages, and the functional solutions I have seen always feel a bit imperative, so I tried to make a more elegant - functional - version in a language I am currently learning: Haskell; this is the result:
I like it; but what do you think? Please comment.
Update
So, after tweeting about this post, I received this reply:
@ToJans @JanVanRyswyck FIzzBuzz in a Tweet #kotlin https://t.co/pvFFrXD6TD :)
— Hadi Hariri (@hhariri) 20 december 2013
Which is apparently about this:
(1..100).forEach{println(when{it%15<1->"FizzBuzz";it%3<1->"Fizz";it%5<1->"Buzz";else->it})}?#Kotlin
— Ziphil :: Antithesis (@Ziphil_) 17 december 2013
So I decided to sacrifice readability, and see how far I could get:
zipWith max(map show[1..100])(zipWith(++)(cycle["","","fizz"])(cycle["","","","","buzz"]))
#FizzBuzz91CharsHaskell
@hhariri @JanVanRyswyck
— Tom Janssens (@ToJans) 20 december 2013
91 Characters! Not bad, but utterly unreadable… So, while it is a good exercise to try to reduce the code to an absolute minimum, this should only be for learning purposes…