import Data Char toUpper import Control Monad when type Todo String ty

 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import Data.Char (toUpper)
import Control.Monad (when)
type Todo = String
type TodoList = [Todo]
numberList :: TodoList -> [String]
numberList = zipWith (\a b -> show a ++ ". " ++ b) [1..]
removeElem :: Int -> [a] -> [a]
removeElem i xs = let (as, bs) = splitAt i xs
in as ++ drop 1 bs
printStrs :: [String] -> IO ()
printStrs = mapM_ putStrLn
showTodoList :: TodoList -> String
showTodoList [] = "No entries"
showTodoList xs = unlines xs
addTodo :: TodoList -> Todo -> TodoList
addTodo xs = flip (:) xs
removeTodo :: TodoList -> Int -> TodoList
removeTodo = flip removeElem
prompt :: String -> IO String
prompt xs = putStrLn xs >> getLine
todoOp :: TodoList -> Action -> IO TodoList
todoOp xs View = putStr (showTodoList xs) >> return xs
todoOp xs Add = addTodo xs <$> prompt "What would you like to add?"
todoOp [] Delete = putStrLn (showTodoList []) >> return []
todoOp xs Delete = do
printStrs xs
response <- prompt "Which line do you want to remove?"
removeTodo xs <$> readLn
return (removeTodo xs (read response))
todoOp xs _ = do
putStrLn "Operation not supported"
return xs
data Action = Add | Delete | View
charToAction :: Char -> Maybe Action
charToAction x = case toUpper x of
'A' -> Just Add
'V' -> Just View
'D' -> Just Delete
_ -> Nothing
mainLoop :: TodoList -> IO ()
mainLoop xs = do
putStrLn "Do you wish to [A]dd, [D]elete, [V]iew, or [E]xit? "
c <- getChar
c' <- getChar -- To capture the "Enter" character
when (toUpper c /= 'E') $
case charToAction c of
Nothing -> putStrLn "Unsupported operation" >> mainLoop xs
Just act -> todoOp xs act >> mainLoop xs
-- TESTS IN GHCI --
Ok, one module loaded.
*Main> mainLoop ["pomme", "banane"]
Do you wish to [A]dd, [D]elete, [V]iew, or [E]xit?
a
What would you like to add?
melon
Do you wish to [A]dd, [D]elete, [V]iew, or [E]xit?
v
pomme
banane
Do you wish to [A]dd, [D]elete, [V]iew, or [E]xit?