Last active
June 20, 2016 08:36
-
-
Save jan-matejka/c01aeb2d3b1f317cb5cb41161d148356 to your computer and use it in GitHub Desktop.
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
| data FizzBuzz = FizzBuzz | |
| | JustInt Int | |
| | Fizz | |
| | Buzz | |
| instance Show FizzBuzz where | |
| show FizzBuzz = "FizzBuzz" | |
| show Fizz = "Fizz" | |
| show Buzz = "Buzz" | |
| show (JustInt x) = show x | |
| multipleOf x y = x `mod` y == 0 | |
| fizzbuzz :: [Int] -> [FizzBuzz] | |
| fizzbuzz = map (_fizzbuzz . precalc) | |
| where | |
| _fizzbuzz :: (Int, Bool, Bool) -> FizzBuzz | |
| _fizzbuzz (_, True, False) = Fizz | |
| _fizzbuzz (_, False, True) = Buzz | |
| _fizzbuzz (_, True, True) = FizzBuzz | |
| _fizzbuzz (x, False, False) = JustInt x | |
| precalc :: Int -> (Int, Bool, Bool) | |
| precalc x = (x, x `multipleOf` 3, x `multipleOf` 5) | |
| main = mapM_ (putStrLn . show) $ fizzbuzz [1..] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment