This file contains 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