Created
December 14, 2013 16:32
-
-
Save configurator/7961431 to your computer and use it in GitHub Desktop.
FizzBuzz in Haskell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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