Skip to content

Instantly share code, notes, and snippets.

@dahlia
Last active January 14, 2018 07:00
Show Gist options
  • Save dahlia/4353479a34cf91e3eca2429fcdf7a1c4 to your computer and use it in GitHub Desktop.
Save dahlia/4353479a34cf91e3eca2429fcdf7a1c4 to your computer and use it in GitHub Desktop.
FizzBuzz: algebraic data types help you represent disjoint events as disjoint sets.
#!/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