Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import System.IO
import System.Directory
import Data.List
import Control.Exception
main = do
contents <- readFile "todo.txt"
let todoTasks = lines contents
numberedTasks = zipWith (\n line -> show n ++ " - " ++ line) [0..] todoTasks
putStrLn "There are your TO-DO items:"
mapM_ putStrLn numberedTasks
putStrLn "Which one do you want to delete?"
numberString <- getLine
let number = read numberString
newTodoItems = unlines $ delete (todoTasks !! number) todoTasks
bracketOnError (openTempFile "." "temp")
(\(tempName, tempHandle) -> do
hClose tempHandle
removeFile tempName)
(\(tempName, tempHandle) -> do
hPutStr tempHandle newTodoItems
hClose tempHandle
removeFile "todo.txt"
renameFile tempName "todo.txt")
@mizukmb
Copy link
Author

mizukmb commented Nov 25, 2017

すごいHaskellたのしく学ぼう! 『第9章 もっと入力、もっと出力』より

@mizukmb
Copy link
Author

mizukmb commented Nov 25, 2017

$ cat todo.txt
Iron the dishes
Dust the dog
Append TODO !!
$ ./deletetodo
There are your TO-DO items:
0 - Iron the dishes
1 - Dust the dog
2 - Append TODO !!
Which one do you want to delete?
2
$ cat todo.txt
Iron the dishes
Dust the dog

@mizukmb
Copy link
Author

mizukmb commented Nov 25, 2017

入出力が出始めた途端に手続き型っぽくなってきた

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment