Skip to content

Instantly share code, notes, and snippets.

@markus1189
Last active December 17, 2015 20:49
Show Gist options
  • Save markus1189/5670322 to your computer and use it in GitHub Desktop.
Save markus1189/5670322 to your computer and use it in GitHub Desktop.
import Control.Arrow
import Data.Monoid
-- An implementation of FizzBuzz using monoids
main :: IO ()
main = mapM_ print $ take 10 . map (show &&& fizzbuzz) $ [0..]
-- the actucal fizzbuzz function constructed by 2 helpers below
fizzbuzz :: Int -> String
fizzbuzz = makeFizzBuzz [ 3 %?? "Fizz", 5 %?? "Buzz", 7 %?? "Bazz"]
-- captures the rules for displaying something, given an integer
makeFizzBuzz :: Monoid a => [Int -> a] -> Int -> a
makeFizzBuzz = fmap mconcat . sequence
-- captures the idea of printing only if a boolean expression is true
(%??) :: Monoid a => Int -> a -> Int -> a
m %?? v = \i -> if i `mod` m == 0 then v else mempty
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment