Skip to content

Instantly share code, notes, and snippets.

@gatlin
Last active August 29, 2015 14:01
Show Gist options
  • Save gatlin/623527079b12aca96eac to your computer and use it in GitHub Desktop.
Save gatlin/623527079b12aca96eac to your computer and use it in GitHub Desktop.
Haskell FizzBuzz solution using monoids. I didn't write this but I wanted to separate it from its original article for didactic reasons.
{-# LANGUAGE MonadComprehensions #-}
-- From https://bmark.us/bmark/readable/060063d3dd0330
import Data.Monoid ((<>))
import Data.Maybe (fromMaybe)
import Control.Monad (mapM_)
fizzbuzz x = fromMaybe (show x) $ [ "fizz" | x `rem` 3 == 0 ]
<> [ "buzz" | x `rem` 5 == 0 ]
<> [ "bazz" | x `rem` 7 == 0 ]
main = mapM_ putStrLn [ fizzbuzz i | i <- [1..100] ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment