6 interactWithUser initialList
19 parseCommand :: String -> Either Error Command
20 parseCommand line = case words line of
21 ["quit"] -> Right Quit
22 ["items"] -> Right DisplayItems
23 "add" : item -> Right $ AddItem $ unwords item
24 "remove" : i -> Right $ RemoveItem $ read $ unwords i
25 ["help"] -> Right Help
26 _ -> Left "Unknown command"
28 addItem :: Item -> Items -> Items
31 displayItems :: Items -> String
32 displayItems = unlines . map ("- " ++)
34 removeItem :: Int -> Items -> Either Error Items
36 | i < 0 || i >= length items = Left "Out of range"
37 | otherwise = Right result
38 where (front, back) = splitAt (i + 1) items
39 result = init front ++ back
41 interactWithUser :: Items -> IO ()
42 interactWithUser items = do
44 case parseCommand line of
45 Right DisplayItems -> do
46 putStrLn $ displayItems items
47 interactWithUser items
49 Right (AddItem item) -> do
50 let newItems = addItem item items
52 interactWithUser newItems
54 Right (RemoveItem i) ->
55 case removeItem i items of
57 putStrLn $ "Removed " ++ items !! i
58 interactWithUser newItems
61 interactWithUser items
64 Right Quit -> return ()
72 interactWithUser items
75 putStrLn $ "Error: " ++ err
76 interactWithUser items