Skip to content

Instantly share code, notes, and snippets.

@Porges
Created July 21, 2014 01:52
Show Gist options
  • Save Porges/1692c5c259c43e6ae7e5 to your computer and use it in GitHub Desktop.
Save Porges/1692c5c259c43e6ae7e5 to your computer and use it in GitHub Desktop.
{-# 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