Created
July 22, 2010 00:48
-
-
Save rchowe/485414 to your computer and use it in GitHub Desktop.
The same visual novel program as before, but in haskell
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 (main) where | |
import System (getArgs) | |
import System.IO (hFlush, stdout) | |
-- Runs a novel from a file | |
runNovel :: String -> IO [()] | |
runNovel filename = readFile filename >>= (mapM runLine) | |
. (filter (\x -> not (isBlank x))) | |
. lines | |
where | |
blanks :: [Char] | |
blanks = " \t\r\n" | |
isBlank :: String -> Bool | |
isBlank [] = True | |
isBlank (x:xs) | |
| blanks `includes` x = isBlank xs | |
| otherwise = False | |
where | |
includes :: Eq a => [a] -> a -> Bool | |
includes [] _ = False | |
includes (x:xs) y | |
| x == y = True | |
| otherwise = includes xs y | |
runLine :: String -> IO () | |
runLine [] = return () | |
runLine x | |
| (take 2 x) /= "> " = return () | |
| (last x) /= ' ' = runLine $ x ++ [' '] | |
| otherwise = putStr (strip x) >> hFlush stdout >> getChar >> return () | |
where strip = tail . tail | |
main :: IO () | |
main = do | |
args <- getArgs | |
if (length args) /= 1 | |
then putStrLn usage | |
else runNovel (args!!0) >> return () | |
where | |
usage :: String | |
usage = "Usage: vn [file]" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment