Created
May 5, 2014 18:12
FizzBuzzWhizz
This file contains hidden or 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 Data.Monoid | |
import Data.Maybe | |
fizzBuzzWhizz (x, y, z) a = | |
fromJust $ getFirst $ mconcat $ map First | |
[ x `contains` a | |
, fizz a x <> buzz a y <> whizz a z | |
, Just $ show a | |
] | |
where | |
contains x a = toMaybe (head (show x) `elem` show a) "Fizz" | |
[fizz, buzz, whizz] = map mkRule2 ["Fizz", "Buzz", "Whizz"] | |
mkRule2 str x y = toMaybe (x `rem` y == 0) str | |
toMaybe True a = Just a | |
toMaybe _ _ = Nothing | |
main = do | |
[x, y, z] <- fmap (map read . splitOn ',') getLine | |
mapM_ (putStrLn . fizzBuzzWhizz (x,y,z)) [1..100] | |
splitOn sep ls = | |
case break (== sep) ls of | |
(h, "") -> [h] | |
(h, t) -> h : splitOn sep (tail t) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment