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/6dbdcce81dbb3801e0fd to your computer and use it in GitHub Desktop.
Save philandstuff/6dbdcce81dbb3801e0fd to your computer and use it in GitHub Desktop.
module FizzBuzz where
import Data.Foldable (fold)
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 = maybe (show n) id tagStr
where tagStr = fold [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