haskell - 变量不在范围内:catch
问题描述
我有这个函数可以用这个代码打开一个文件,但它正在返回
teste.hs: 228: 5: error:
Variable not in scope:
catch :: IO Handle -> (t0 -> IO Handle) -> IO Handle
我的代码:
opensArchive :: String -> IOMode -> IO Handle
opensFile file mode =
catch (openFile mode file)
(\ _ -> do {
putStrLn ("Unable to open" ++ file);
putStrLn "It will be opened with a default name: data.txt and cleared";
pt_arq <- opensFile "data.txt" WriteMode;
closeArchive pt_arq;
opensFile "data.txt" ReadMode
}
)
closeArchive :: Handle -> IO ()
closeFile handle_arq = hClose handle_arq
解决方案
您可能想查看 function 的规范catch
。
冲浪到Hoogle。然后在网页左上角的搜索栏中输入“catch”。
然后单击顶部解决方案。这将使您进入function的规范网页catch
。
在该页面的最顶部,您会看到它是关于 moduleControl.Exception
的。你导入了吗?
测试下ghci
:
$ ghci
GHCi, version 8.6.5: http://www.haskell.org/ghc/ :? for help
...
λ>
λ> :type catch
<interactive>:1:1: error: Variable not in scope: catch
λ>
λ> import Control.Exception
λ>
λ> :type catch
catch :: Exception e => IO a -> (e -> IO a) -> IO a
λ>
λ>
瞧!
推荐阅读
- firebase - 如何在 Firestore 集合中检索文档的 documentId,以便在返回对象流的 getter 中使用它
- c++ - 单例和新运算符 - 线程安全
- c - 如何在C中清除控制台而不闪烁
- python - 使用 python 从 Cassandra 数据库中检索数据
- database - 我可以为 MongoDB 索引配置特定路径吗?
- html - 如何对齐网格系统html/css?
- docker - 无法到达 docker 容器 - 端口未绑定
- html - 响应式列,防止孩子在折叠时在列之间移动
- javascript - 在 mapbox 中读取 GPX 文件或第三方 URL 以添加行
- python - 为什么这个带有浮点除法的 Django 查询注释这么慢?