Skip to content

Instantly share code, notes, and snippets.

@deque-blog
Last active December 31, 2016 15:28
Embed
What would you like to do?
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