Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@orkoden
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orkoden/035a68afd5c7674f5ec4 to your computer and use it in GitHub Desktop.
Save orkoden/035a68afd5c7674f5ec4 to your computer and use it in GitHub Desktop.
FizzBuzz in Haskell that I live coded as part of a demo for hackership
data Fizzbuzz = FizzBuzz
| Fizz
| Buzz
deriving Show
fizzbuzz :: [Integer] -> String
fizzbuzz x = unlines (map fizzbuzzline x)
fizzbuzzline :: Integer -> String
fizzbuzzline x = fizzBuzzToString x (fizzBuzzFromNum x)
fizzBuzzToString :: Integer -> Maybe Fizzbuzz -> String
fizzBuzzToString _ (Just fizzorbuzz) = show fizzorbuzz
fizzBuzzToString x Nothing = show x
fizzBuzzFromNum :: Integer -> Maybe Fizzbuzz
fizzBuzzFromNum x
| divby x 3 && divby x 5 = Just FizzBuzz
| divby x 3 = Just Fizz
| divby x 5 = Just Buzz
| otherwise = Nothing
divby :: Integer -> Integer -> Bool
divby x y = x `mod` y == 0
main = putStrLn $ fizzbuzz [1..100]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment