Report a paste

Please put a quick comment for the admin.

If it looks like spam, the admin will mark it as spam so that the spam filter picks it up in the future.

If the paste contains something private or offensive, it'll probably just be deleted.

Generalized FizzBuzz

{-# LANGUAGE RankNTypes #-}
import Control.Applicative ((<|>))

type FizzBuzz = forall a . (Integral a, Show a) => a -> String

mkFizzBuzz :: (forall a . (Integral a, Show a) => [(a, String)]) -> FizzBuzz
mkFizzBuzz l n = if null fizzed then show n else fizzed
  where
    fizzed = mconcat (map fun l) n
    fun (d, s) x
        | x `mod` d == 0 = s
        | otherwise = mempty

standard :: FizzBuzz
standard = mkFizzBuzz [(3, "Fizz"), (5, "Buzz")]

custom :: FizzBuzz
custom = mkFizzBuzz [(3, "Fizz"), (5, "Buzz"), (7, "Xyzzy"), (13, "Quux")]

main :: IO ()
main = do
    putStrLn "Standard:"
    mapM_ (putStrLn . standard) [1..100]
    putStrLn "Custom:"
    mapM_ (putStrLn . custom) [1..100]