Created
December 1, 2013 13:46
-
-
Save cohama/7733858 to your computer and use it in GitHub Desktop.
すごい Haskell 9.5 もっと ToDo リスト
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
import System.Environment | |
import System.Directory | |
import System.IO | |
import Data.List | |
import Control.Exception | |
dispatch :: String -> [String] -> IO () | |
dispatch "add" = add | |
dispatch "view" = view | |
dispatch "remove" = remove | |
main :: IO () | |
main = do | |
(command:argList) <- getArgs | |
dispatch command argList | |
add :: [String] -> IO () | |
add [fileName, todoItem] = appendFile fileName (todoItem ++ "\n") | |
view :: [String] -> IO () | |
view [fileName] = do | |
contents <- readFile fileName | |
let todoTasks = lines contents | |
numberedTasks = zipWith (\n line -> show n ++ " - " ++ line) [0..] todoTasks | |
putStr $ unlines numberedTasks | |
remove :: [String] -> IO () | |
remove [fileName, numberString] = do | |
contents <- readFile fileName | |
let todoTasks = lines contents | |
numberedTasks = zipWith (\n line -> show n ++ " - -" ++ line) [0..] todoTasks | |
putStrLn "These are your TO-DO items:" | |
mapM_ putStrLn numberedTasks | |
let number = read numberString | |
newTodoItem = unlines $ delete (todoTasks !! number) todoTasks | |
bracketOnError (openTempFile "." "temp") | |
(\(tempName, tempHandle) -> do | |
hClose tempHandle | |
removeFile tempName) | |
(\(tempName, tempHandle) -> do | |
hPutStr tempHandle newTodoItem | |
hClose tempHandle | |
removeFile "todo.txt" | |
renameFile tempName "todo.txt") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment