Re:FizzBuzz in Too Much Detail
by wz1000 on Sunday April 26, 2015 @10:15AM
(#9441823)
Here's a version in Haskell that has the same functionality:
Its just me playing around and trying to be clever.The way FizzBuzz can be expressed as a Monoid is cool. The rest is just plumbing.
Note that this requires {-# LANGUAGE MonadComprehensions #-} to compile. Thats for the sugar used when constructing the triggers.
[ Parent
| on HN ]
If you're going to use the (->) Monad instance, why not:
{-# LANGUAGE MonadComprehensions #-}
import Data.Maybe (fromMaybe)
import Data.Monoid (mconcat)
import Control.Applicative ((<*>))
pam = flip map
fizzbuzz i trigs = pam [i..] $ fromMaybe . show <*> mconcat . trigs
ans = fizzbuzz 1 $ \i -> [["Fizz" | i `rem` 3 == 0]
,["Buzz" | i `rem` 5 == 0]
]
main = mapM_ putStrLn $ take 100 ansI'm sure that Haskell can do better than that in the readability department.
It can, I was just trying to be clever :P
For one, I don't need to drag monads into function composition:
fizzbuzz i triggers = map (\x -> fromMaybe (show x) $
mconcat $ triggers x)
[i..]
fizzbuzz 1 $ \i -> [["Fizz" | i `rem` 3 == 0]
,["Buzz" | i `rem` 5 == 0]
]