Thoughts, Rant and Code

Fizz Buzz in Haskell - Power of Filter and Map

Off late, I have been learning Haskell programming language. Suddenly the thought of how Fizz Buzz would look like in Haskell strike my mind.

After 5 minutes, a working version using guards and map was ready.

fizzBuzz :: Int -> [Char]
fizzBuzz n
  | mod n 15 == 0 = "FizzBuzz"
  | mod n 3  == 0 = "Fizz"
  | mod n 5 == 0 = "Buzz"
  | otherwise = show n

Then simply mapping the function to a range of first 100 was enough.

map fizzBuzz [1..100]

The beauty is the ability to chain multiple functions and get a working program. More or less the similar is possible in imperative languages as well. But, imagine a scenario where one need to know how many numbers are there which are divisible by both 3 and 5. Same can be obtained from the FizzBuzz function, just by chaining two more functions.

This may be a bad example of reusing function, but one gets the point. Just like the *NIX shell’s ability to chain multiple programs and get the output quickly and elegantly.

length (filter (=="FizzBuzz") (map fizzBuzz [1..100]))