Skip to content

Instantly share code, notes, and snippets.

@philandstuff
Forked from purcell/FizzBuzz.hs
Last active August 29, 2015 14:08
Show Gist options
  • Save philandstuff/ef740b11470a17bf9bb5 to your computer and use it in GitHub Desktop.
Save philandstuff/ef740b11470a17bf9bb5 to your computer and use it in GitHub Desktop.
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