-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.hs
37 lines (33 loc) · 1.04 KB
/
todo.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
putTodo :: (Int, String) -> IO ()
putTodo (n, todo) = putStrLn (show n ++ ": " ++ todo)
prompt :: [String] -> IO ()
prompt todos = do
putStrLn ""
putStrLn "Your current todo list:"
mapM_ putTodo (zip [0..] todos)
command <- getLine
interpret command todos
interpret :: String -> [String] -> IO ()
interpret ('+':' ':todo) todos = prompt (todo:todos)
interpret ('-':' ':num ) todos =
case delete (read num) todos of
Nothing -> do
putStrLn "No todo entry matches the given number"
prompt todos
Just todos' -> prompt todos'
interpret "q" todos = return ()
interpret command todos = do
putStrLn ("Invalid command: `" ++ command ++ "`")
prompt todos
delete :: Int -> [a] -> Maybe [a]
delete 0 (_:as) = Just as
delete n (a:as) = do
as' <- delete (n - 1) as
return (a:as')
delete _ [] = Nothing
main = do
putStrLn "Commands:"
putStrLn "+ <String> - Add a entry"
putStrLn "- <Int> - Delete the numbered entry"
putStrLn "q - Quit"
prompt []