X-Git-Url: http://git.lukelau.me/?a=blobdiff_plain;f=test%2Ffiles%2Fsimple.hs;fp=test%2Ffiles%2Fsimple.hs;h=f58bbd036b2321072289bd951ebac7749252cce0;hb=e728814eed6134acf8281a1ad08eecaf438a736a;hp=0000000000000000000000000000000000000000;hpb=ad24be51d5cb2445e8a6a8216a6c8e580447439a;p=lsp-test.git diff --git a/test/files/simple.hs b/test/files/simple.hs new file mode 100644 index 0000000..f58bbd0 --- /dev/null +++ b/test/files/simple.hs @@ -0,0 +1,76 @@ +module Main where + +main :: IO () +main = do + let initialList = [] + interactWithUser initialList + +type Item = String +type Items = [Item] + +data Command = Quit + | DisplayItems + | AddItem String + | RemoveItem Int + | Help + +type Error = String + +parseCommand :: String -> Either Error Command +parseCommand line = case words line of + ["quit"] -> Right Quit + ["items"] -> Right DisplayItems + "add" : item -> Right $ AddItem $ unwords item + "remove" : i -> Right $ RemoveItem $ read $ unwords i + ["help"] -> Right Help + _ -> Left "Unknown command" + +addItem :: Item -> Items -> Items +addItem = (:) + +displayItems :: Items -> String +displayItems = unlines . map ("- " ++) + +removeItem :: Int -> Items -> Either Error Items +removeItem i items + | i < 0 || i >= length items = Left "Out of range" + | otherwise = Right result + where (front, back) = splitAt (i + 1) items + result = init front ++ back + +interactWithUser :: Items -> IO () +interactWithUser items = do + line <- getLine + case parseCommand line of + Right DisplayItems -> do + putStrLn $ displayItems items + interactWithUser items + + Right (AddItem item) -> do + let newItems = addItem item items + putStrLn "Added" + interactWithUser newItems + + Right (RemoveItem i) -> + case removeItem i items of + Right newItems -> do + putStrLn $ "Removed " ++ items !! i + interactWithUser newItems + Left err -> do + putStrLn err + interactWithUser items + + + Right Quit -> return () + + Right Help -> do + putStrLn "Commands:" + putStrLn "help" + putStrLn "items" + putStrLn "add" + putStrLn "quit" + interactWithUser items + + Left err -> do + putStrLn $ "Error: " ++ err + interactWithUser items