Skip to content

Instantly share code, notes, and snippets.

@susisu
Created August 5, 2015 16:20
Show Gist options
  • Save susisu/7eae2f11f8bfd76be2a3 to your computer and use it in GitHub Desktop.
Save susisu/7eae2f11f8bfd76be2a3 to your computer and use it in GitHub Desktop.
HQ9F+
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