Skip to content

Instantly share code, notes, and snippets.

@Punie
Created July 30, 2019 12:27
Show Gist options
  • Select an option

  • Save Punie/a68a0240916b808074509b604113e013 to your computer and use it in GitHub Desktop.

Select an option

Save Punie/a68a0240916b808074509b604113e013 to your computer and use it in GitHub Desktop.
#!/usr/bin/env stack
-- stack --resolver lts-11.11 --install-ghc runghc
{-# LANGUAGE ScopedTypeVariables #-}
import Data.Foldable
import Data.Maybe (fromMaybe)
type FizzRule = Integer -> Maybe String
rule :: Integer -> String -> FizzRule
rule n m i =
case i `mod` n of
0 -> Just m
_ -> Nothing
fizz :: FizzRule
fizz = rule 3 "Fizz"
buzz :: FizzRule
buzz = rule 5 "Buzz"
fizzBuzz :: (Functor f, Foldable t)
=> t (Integer -> Maybe String)
-> f Integer
-> f String
fizzBuzz rules = fmap (fromMaybe <$> show <*> ruleSet)
where
ruleSet = fold rules
main :: IO ()
main = do
putStr "Provide some integer: "
(n :: Integer) <- read <$> getLine
let res = fizzBuzz [fizz, buzz] [1..n]
mapM_ putStrLn res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment