Skip to content

Instantly share code, notes, and snippets.

@jan-matejka
Last active June 20, 2016 08:36
Show Gist options
  • Select an option

  • Save jan-matejka/c01aeb2d3b1f317cb5cb41161d148356 to your computer and use it in GitHub Desktop.

Select an option

Save jan-matejka/c01aeb2d3b1f317cb5cb41161d148356 to your computer and use it in GitHub Desktop.
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