Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.