Last active
August 29, 2015 14:15
-
-
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
This file contains 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 | |
| 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