Created
May 13, 2018 02:36
-
-
Save n4to4/3394e051d67d6d7a8d2bacd9cc8d0252 to your computer and use it in GitHub Desktop.
fizzbuzz.hs
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
module Main where | |
import Data.Foldable (asum) | |
import Data.Maybe (fromMaybe) | |
toMaybe :: Bool -> a -> Maybe a | |
toMaybe False _ = Nothing | |
toMaybe _ value = Just value | |
fizzbuzz :: Integer -> String | |
fizzbuzz = fromMaybe <$> show <*> asum . sequence rules | |
where | |
fb m output n = toMaybe (n `mod` m == 0) output | |
rules = [fb 15 "FizzBuzz", fb 3 "Fizz", fb 5 "Buzz"] | |
main :: IO () | |
main = print $ map fizzbuzz [1..100] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment