免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3303 | 回复: 2
打印 上一主题 下一主题

请教 haskell 中的一个问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-01-12 18:09 |只看该作者 |倒序浏览
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   代表什么意思?  是句柄 还是函数??? 谢谢.

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
2 [报告]
发表于 2009-01-12 21:31 |只看该作者
原帖由 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.
复制代码

论坛徽章:
0
3 [报告]
发表于 2009-01-13 15:16 |只看该作者
谢谢.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP