Chinaunix

标题: 请教 haskell 中的一个问题 [打印本页]

作者: luckyelement    时间: 2009-01-12 18:09
标题: 请教 haskell 中的一个问题
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   代表什么意思?  是句柄 还是函数??? 谢谢.
作者: flw    时间: 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.
复制代码

作者: luckyelement    时间: 2009-01-13 15:16
谢谢.




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2