summaryrefslogtreecommitdiff
path: root/src/File.hs
blob: 01b33af4b762a3fa8640b4f21da5911294098b25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
module File(
    countLines
    ) where




import qualified System.IO as IO




countLines :: FilePath -> IO Int
countLines f = do
    h <- IO.openFile f IO.ReadMode
    e <- IO.hIsEOF h
    if e
        then IO.hClose h >> return 0
        else countLinesTail h 0




countLinesTail :: IO.Handle -> Int -> IO Int
countLinesTail h n = do
    t <- IO.hGetLine h
    e <- IO.hIsEOF h
    if e
        then IO.hClose h >> return (n + 1)
        else countLinesTail h (n + 1)