Skip to content

Instantly share code, notes, and snippets.

@deque-blog
Last active December 31, 2016 15:28
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 deque-blog/0f6b9bdf3b4aba69d9a127b6dae5d268 to your computer and use it in GitHub Desktop.
Save deque-blog/0f6b9bdf3b4aba69d9a127b6dae5d268 to your computer and use it in GitHub Desktop.
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