Created
July 21, 2014 01:52
-
-
Save Porges/1692c5c259c43e6ae7e5 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
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | |
import Data.Monoid | |
newtype On = On [(Int, String)] deriving Monoid | |
on n s = On [(n, s)] | |
messages (On x) = cycle $ map funcFor [1..maxM] | |
where | |
maxM = foldl1 lcm $ map fst x | |
funcFor m = case buildMessage of | |
[] -> show | |
s -> const s | |
where buildMessage = concat [ s | (n, s) <- x, m `mod` n == 0 ] | |
printMessages = mapM_ (\(n, f) -> putStrLn $ f n) . zip [1..] | |
run = printMessages . messages | |
main = run $ on 3 "Fizz" <> on 5 "Buzz" <> on 4 "Kak" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment