Last active
December 31, 2016 15:28
-
-
Save deque-blog/0f6b9bdf3b4aba69d9a127b6dae5d268 to your computer and use it in GitHub Desktop.
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
fizzBuzz :: Int -> String | |
fizzBuzz 0 = "0" | |
fizzBuzz n = | |
let res = fizzBuzzImpl [newRule 3 "Fizz", newRule 5 "Buzz"] n | |
in if res == "" | |
then show n | |
else res | |
type Rule = Int -> String | |
newRule :: Int -> String -> Rule | |
newRule divisor out n | |
| isMultiple n divisor = out | |
| otherwise = "" | |
fizzBuzzImpl :: [Rule] -> Int -> String | |
fizzBuzzImpl rules n = concatMap ($ n) rules | |
isMultiple :: Int -> Int -> Bool | |
isMultiple n divisor = mod n divisor == 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment