Skip to content

Instantly share code, notes, and snippets.

@configurator
Created December 14, 2013 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save configurator/7961431 to your computer and use it in GitHub Desktop.
Save configurator/7961431 to your computer and use it in GitHub Desktop.
FizzBuzz in Haskell
import Control.Applicative
options = [(3, "Fizz"), (5, "Buzz")]
collect :: [(Integer, String)] -> Integer -> String
collect [] x = ""
collect ((divisor,string):rest) x
| x `mod` divisor == 0 = string ++ collect rest x
| otherwise = collect rest x
collectWithDefault :: [(Integer, String)] -> Integer -> String
collectWithDefault options x =
case result of
"" -> show x
_ -> result
where result = collect options x
main :: IO ()
main =
putStrLn $ unlines $ map (collectWithDefault options) [1..100]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment