Saturday 4 June 2016

Haskell: Count Number of words in a file


wordCount.hs
main = interact wordCount
       where wordCount input = show (length (words 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 wordCount < input.txt
74


Previous                                                 Next                                                 Home

No comments:

Post a Comment