Created
August 5, 2015 16:20
-
-
Save susisu/7eae2f11f8bfd76be2a3 to your computer and use it in GitHub Desktop.
HQ9F+
This file contains hidden or 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 Control.Monad | |
import Control.Exception | |
import Data.IORef | |
main :: IO () | |
main = loop `catch` ignore | |
where | |
loop :: IO () | |
loop = forever $ do | |
acc <- newIORef 0 | |
src <- getLine | |
forM_ src $ \com -> run com src acc | |
ignore :: SomeException -> IO () | |
ignore _ = return () | |
run :: Char -> String -> IORef Int -> IO () | |
run com src acc = | |
case com of | |
'H' -> putStrLn "Hello, world!" | |
'Q' -> putStrLn src | |
'9' -> beer 99 | |
'F' -> fizzbuzz | |
'+' -> modifyIORef acc (+ 1) | |
_ -> return () | |
beer :: Int -> IO () | |
beer 1 = | |
let text = | |
"1 bottle of beer\n" ++ | |
"you take it down, pass it around,\n" ++ | |
"no more bottles of beer on the wall.\n" | |
in putStrLn text | |
beer n = | |
let text = | |
show n ++ " bottles of beer\n" ++ | |
"you take one down, pass it around,\n" ++ | |
show (n - 1) ++ " bottles of beer on the wall.\n" | |
in do | |
putStrLn text | |
beer (n - 1) | |
fizzbuzz :: IO () | |
fizzbuzz = | |
forM_ [1 .. 100] $ \i -> | |
if i `mod` 15 == 0 then | |
putStrLn "FizzBuzz" | |
else if i `mod` 3 == 0 then | |
putStrLn "Fizz" | |
else if i `mod` 5 == 0 then | |
putStrLn "Buzz" | |
else | |
print i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment