Last active
January 14, 2018 07:00
-
-
Save dahlia/4353479a34cf91e3eca2429fcdf7a1c4 to your computer and use it in GitHub Desktop.
FizzBuzz: algebraic data types help you represent disjoint events as disjoint sets.
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
#!/usr/bin/env runhaskell | |
module FizzBuzz where | |
import Control.Monad | |
data FizzBuzz = Otherwise Int | Fizz | Buzz | FizzBuzz deriving (Show) | |
fizzBuzz :: Int -> FizzBuzz | |
fizzBuzz i = case (i `mod` 3 == 0, i `mod` 5 == 0) of | |
(False, False) -> Otherwise i | |
(True, False) -> Fizz | |
(False, True) -> Buzz | |
(True, True) -> FizzBuzz | |
main :: IO () | |
main = forM_ [1..100] $ print . fizzBuzz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment