Created
January 14, 2012 02:47
-
-
Save ymirpl/1610035 to your computer and use it in GitHub Desktop.
file 'appending' in haskell
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
main = do | |
handle <- openFile "todo.txt" ReadMode | |
(tempName, tempHandle) <- openTempFile "." "temp" | |
contents <- hGetContents handle | |
let todoTasks = lines contents | |
numberedTasks = zipWith (\n line -> show n ++ " - " ++ | |
line) [0..] todoTasks | |
putStrLn "These are your TO-DO items:" | |
putStr $ unlines numberedTasks | |
putStrLn "Which one do you want to delete?" | |
numberString <- getLine | |
let number = read numberString | |
newTodoItems = delete (todoTasks !! number) todoTasks | |
hPutStr tempHandle $ unlines newTodoItems | |
hClose handle | |
hClose tempHandle | |
removeFile "todo.txt" | |
renameFile tempName "todo.txt" | |
-- some IO / String fiddle | |
import System.IO | |
main :: IO () | |
main = do | |
inh <- openFile "sampleEvents" ReadMode | |
inContents <- getContentsHereInstead inh | |
putStrLn inContents | |
hClose inh | |
getContentsHereInstead :: Handle -> IO String | |
getContentsHereInstead handle = do | |
inContents <- hGetContents handle | |
return inContents |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment