blob: 5e4dce83f288536ad3af1ace181fc39c5aa981db (
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
33
34
35
36
37
38
39
40
41
42
|
module File(
countLines
) where
-- This source is licensed under Creative Commons CC0 v1.0.
-- To read the full text, see license.txt in the main directory of this repository
-- or go to https://creativecommons.org/publicdomain/zero/1.0/legalcode.txt
-- For a human readable summary, go to https://creativecommons.org/publicdomain/zero/1.0/
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)
|