Haskell初心者です。本のコードを少し変えて写したのですがエラーが出て解決できません。
todoリストで、コマンドライン引数で値を渡して、テキストファイルに入出力をするプログラムです。コマンドラインに渡す形式としては以下です。removenに渡している「2」で消したい番号を指定してます。
bash
1./todo add todo.txt "予定などのTodo" 2./todo view todo.txt 3./todo remove todo.txt 2
エラー内容は以下です。
todo.hs:33:5: error: Variable not in scope: bracketOnError :: IO (FilePath, Handle) -> ((FilePath, Handle) -> IO ()) -> ((FilePath, Handle) -> IO ()) -> IO ()
ソースコードの全文です。エラー個所に ERROR とコメントをつけておきました
Haskell
1{-# OPTIONS -Wall #-} 2import System.IO 3import System.Environment 4import System.Directory 5import Data.List 6 7main = do 8 (command:argList) <- getArgs 9 dispatch command argList 10 11dispatch :: String -> [String] -> IO () 12dispatch "add" = add 13dispatch "view" = view 14dispatch "remove" = remove 15dispatch _ = error "no match method" 16 17add :: [String] -> IO() 18add [fileName, todoItem] = appendFile fileName (todoItem ++ "\n") 19 20view :: [String] -> IO () 21view [fileName] = do 22 contents <- readFile fileName 23 let todoTasks = lines contents 24 numberedTasks = zipWith (\n line -> show n ++ " " ++ line) [0..] todoTasks 25 putStrLn $ unlines numberedTasks 26 27remove :: [String] -> IO() 28remove [fileName, numberString] = do 29 contents <- readFile fileName 30 let number = read numberString 31 oldTodoItems = zipWith (\n line -> show n ++ " " ++ line) [0..] $ lines contents 32 newTodoItems = unlines $ delete (oldTodoItems !! number) oldTodoItems 33 bracketOnError {- ERROR -} 34 (openTempFile "." "temp") 35 (\ (tempName, tempHandle) -> do 36 hClose tempHandle 37 removeFile tempName) 38 (\ (tempName, tempHandle) -> do 39 hPutStr tempHandle newTodoItems 40 hClose tempHandle 41 removeFile fileName 42 renameFile tempName fileName)

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。