请教 haskell 中的一个问题

发布于 2022-08-18 14:20:16 字数 629 浏览 17 评论 2

1. 统计一个文件的行数
2. 代码如下:

import IO
   
main = do
        l <- countLineFile "d:/downloads/putstr.hs"
        putStrLn (show l)
       
countLineFile f = do
        h <- openFile f ReadMode
        hCountLines h

hCountLines h = do
        x <- hGetContents h
        return (length (lines x))

请教 hGetContents   代表什么意思?  是句柄 还是函数??? 谢谢.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奈何桥上唱咆哮 2022-08-20 06:40:18

谢谢.

风流物 2022-08-20 00:41:40

原帖由 luckyelement 于 2009-1-12 18:09 发表
1. 统计一个文件的行数
2. 代码如下:

import IO
   
main = do
        l  

你可以查一下文档。
http://haskell.org/hoogle/?hoogle=hGetContents
搜索结果第二个链接打开:
http://haskell.org/ghc/docs/late ... html#v:hGetContents

  1. hGetContents :: Handle -> IO String
  2. Computation hGetContents hdl returns the list of characters corresponding to the unread portion of the channel or file managed by hdl, which is put into an intermediate state, semi-closed. In this state, hdl is effectively closed, but items are read from hdl on demand and accumulated in a special list returned by hGetContents hdl.
  3. Any operation that fails because a handle is closed, also fails if a handle is semi-closed. The only exception is hClose. A semi-closed handle becomes closed:
  4.     * if hClose is applied to it;
  5.     * if an I/O error occurs when reading an item from the handle;
  6.     * or once the entire contents of the handle has been read.
  7. Once a semi-closed handle becomes closed, the contents of the associated list becomes fixed. The contents of this final list is only partially specified: it will contain at least all the items of the stream that were evaluated prior to the handle becoming closed.
  8. Any I/O errors encountered while a handle is semi-closed are simply discarded.
  9. This operation may fail with:
  10.     * isEOFError if the end of file has been reached.

复制代码

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文