Skip to content

Instantly share code, notes, and snippets.

@n4to4
Created May 13, 2018 02:36
Show Gist options
  • Save n4to4/3394e051d67d6d7a8d2bacd9cc8d0252 to your computer and use it in GitHub Desktop.
Save n4to4/3394e051d67d6d7a8d2bacd9cc8d0252 to your computer and use it in GitHub Desktop.
fizzbuzz.hs
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