Skip to content

Instantly share code, notes, and snippets.

@purcell
Created October 30, 2014 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save purcell/26721f84bcecce27805e to your computer and use it in GitHub Desktop.
Save purcell/26721f84bcecce27805e to your computer and use it in GitHub Desktop.
fizzbuzz
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