Last active
December 17, 2015 20:49
-
-
Save markus1189/5670322 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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