Saturday, 4 June 2016

Haskell: Count number of lines in a file


countLines.hs
main = interact wordCount
    where wordCount input = show (length (lines input)) ++ "\n"

Suppose input.txt has following data.


input.txt
Friendship can turn into enmity, and enmity can become friendship. 
You all know — happiness can turn into sadness, and sadness can change into happiness. 
Although they are polar opposites, they are almost like twins, very close. 
Just a slight change in circumstances and one disappears …the other was just behind it. 
So remember the transcendent — existence belongs to the transcendental. 
Don’t divide it; otherwise you will be continuously tortured by the duality.

$ runghc countLines < input.txt
6



Previous                                                 Next                                                 Home

No comments:

Post a Comment