/ Published in: Haskell
The dread FizzBuzz question -- really, a test if your average programmer knows his or her FOR loops. The spec is that you're counting from 1 to 100. Your program should print out "fizz" if the index is divisible by three, and "buzz" if it's divisible by five, and "fizzbuzz" if it's divisible by both.
I have a couple different versions in this code: first, a few functions just fizzing or buzzing. Second, a generalization which allows any standard message against any divisor. Third, a purely functional version that zips two lists together (giving us free concatenation for "fizzbuzz"). Fourth, a list comprehension and lastly, a monadic version that calls a pure function that uses guards.
I have a couple different versions in this code: first, a few functions just fizzing or buzzing. Second, a generalization which allows any standard message against any divisor. Third, a purely functional version that zips two lists together (giving us free concatenation for "fizzbuzz"). Fourth, a list comprehension and lastly, a monadic version that calls a pure function that uses guards.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
-- file: Fizz.hs -- a Haskell implementation of the fizzbuzz problem -- a generalized version. It takes the index and the value to divide against, and -- returns the message if n is evenly divisible by x -- a purely functional implementation. -- another purely functional version. Very easy to remember. -- List comprehensions, anyone? boomBang = then "boombang" then "boom" then "bang" else show x | x <- ns] -- the answer your recruiter is probably looking for -- (if your recruiter has enough doubts about your -- programming ability that he/she busts out -- fizzbuzz on your butt) where fizz' n -- or, to get rid of the explicit recursion in the main routine where