Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
module FizzBuzz where
import Data.Maybe
data FizzBuzz = Fizz | Buzz deriving Show
whenDivisible :: Integral a => a -> b -> a -> Maybe b
whenDivisible d x n = if n `rem` d == 0 then Just x else Nothing
fizzbuzz :: (Show a, Integral a) => a -> String
fizzbuzz n = if null tags then show n else concatMap show tags
where tags = catMaybes [fizz n, buzz n]
fizz = whenDivisible 3 Fizz
buzz = whenDivisible 5 Buzz
main = mapM_ (putStrLn . fizzbuzz) [1..50]
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.