Online Book Reader

Home Category

Learn You a Haskell for Great Good! - Miran Lipovaca [80]

By Root 455 0
= lines contents

numberedTasks = zipWith (\n line -> show n ++ " - " ++ line)

[0..] todoTasks

putStrLn "These 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

(tempName, tempHandle) <- openTempFile "." "temp"

hPutStr tempHandle newTodoItems

hClose tempHandle

removeFile "todo.txt"

renameFile tempName "todo.txt"

First, we read todo.txt and bind its contents to contents. Then we split the contents into a list of strings, with one line for each string. So todoTasks is now something like this:

["Iron the dishes", "Dust the dog", "Take salad out of the oven"]

We zip the numbers from 0 onward and that list with a function that takes a number (like 3) and a string (like "hey") and returns a new string (like "3 - hey"). Now numberedTasks looks like this:

["0 - Iron the dishes"

,"1 - Dust the dog"

,"2 - Take salad out of the oven"

]

We then use mapM_ putStrLn numberedTasks to print each task on a separate line, ask the user which one to delete, and wait for the user to enter a number. Let’s say we want to delete number 1 (Dust the dog), so we punch in 1. numberString is now "1", and because we want a number rather than a string, we apply read to that to get 1 and use a let to bind that to number.

Remember the delete and !! functions from Data.List? !! returns an element from a list with some index. delete deletes the first occurrence of an element in a list and returns a new list without that occurrence. (todoTasks !! number) results in "Dust the dog". We delete the the first occurrence of "Dust the dog" from todoTasks and then join that into a single line with unlines and name that newTodoItems.

Then we use a function that we haven’t met before, from System.IO: openTempFile. Its name is pretty self-explanatory. It takes a path to a temporary directory and a template name for a file and opens a temporary file. We used "." for the temporary directory, because . denotes the current directory on just about any operating system. We used "temp" as the template name for the temporary file, which means that the temporary file will be named temp plus some random characters. It returns an I/O action that makes the temporary file, and the result in that I/O action is a pair of values: the name of the temporary file and a handle. We could just open a normal file called todo2.txt or something like that, but it’s better practice to use openTempFile so you know you’re probably not overwriting anything.

Now that we have a temporary file opened, we write newTodoItems to it. The old file is unchanged, and the temporary file contains all the lines that the old one does, except the one we deleted.

After that, we close both the original and the temporary files, and remove the original one with removeFile, which takes a path to a file and deletes it. After deleting the old todo.txt, we use renameFile to rename the temporary file to todo.txt. removeFile and renameFile (which are both in System.Directory) take file paths, not handles, as their parameters.

Save this as deletetodo.hs, compile it, and try it:

$ ./deletetodo

These are your TO-DO items:

0 - Iron the dishes

1 - Dust the dog

2 - Take salad out of the oven

Which one do you want to delete?

1

Now let’s see which items remain:

$ cat todo.txt

Iron the dishes

Take salad out of the oven

Ah, cool! Let’s delete one more item:

$ ./deletetodo

These are your TO-DO items:

0 - Iron the dishes

1 - Take salad out of the oven

Which one do you want to delete?

0

And examining the file, we see that only one item remains:

$ cat todo.txt

Take salad out of the oven

So, everything is working. However, there’s one thing that about this program that’s kind of off. If something goes wrong after we open our temporary file, the program terminates, but the temporary file doesn’t get cleaned up. Let’s remedy that.

Cleaning Up


To make sure our temporary file is cleaned up in case of a problem, we’re going to

Return Main Page Previous Page Next Page

®Online Book Reader