Skip to content

Instantly share code, notes, and snippets.

@agausmann
Last active January 2, 2017 20:52
Show Gist options
  • Save agausmann/fd3d083b7d24c6b5f0656905ceca27fa to your computer and use it in GitHub Desktop.
Save agausmann/fd3d083b7d24c6b5f0656905ceca27fa to your computer and use it in GitHub Desktop.
import System.Environment
import System.IO
import Data.List
main = do
(command:args) <- getArgs
case command of
"view" -> view args
"add" -> add args
"remove" -> remove args
readEntries :: String -> IO [String]
readEntries fileName = do
contents <- readFile fileName
return $ lines contents
writeEntries :: String -> [String] -> IO ()
writeEntries fileName entries = writeFile fileName $ unlines entries
view :: [String] -> IO ()
view [fileName] = do
entries <- readEntries fileName
putStr $ unlines $ zipWith (\n entry -> (show n) ++ " - " ++ entry) [0..] entries
add :: [String] -> IO ()
add [fileName, entry] = appendFile fileName (entry ++ "\n")
remove :: [String] -> IO ()
remove [fileName, index] = do
entries <- readEntries fileName
writeEntries fileName $ delete (entries!!(read index :: Int)) entries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment