Haskell中的字数统计实用程序

时间:2014-12-06 16:12:17

标签: haskell count lines word-count

我对Haskell相当新鲜。我一直在尝试创建一个实用程序来计算Haskell中的单词和行数,以帮助我更好地理解语言。但是,我正在努力让它发挥作用。

到目前为止,我有:

wordCountUtility = do

       putStrLn "Please enter the filename:"

       filename <- getLine

       putStrLn ("The file name you have entered is: " ++ filename)

      contents <- readFile filename -- read the file specified in “name” into “contents”

      lower <- (return . map toLower) contents

      putStrLn lower

我曾尝试使用'chop'并找到print . length . words =<< getContents并对其进行了多次修改,但我没有运气。 我还看了很多关于Stack Overflow的类似答案,例如:identifying number of words in a paragraph using haskell

输出应该与此类似:

Amount of Lines within the file
Lines : 10

Amount of Words found within the file
Words : 110

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

你的wordCountUtility可能还没有计算单词。你应该停下来像

commandLineUtility :: (String -> String) -> IO ()
commandLineUtility fn = do
       putStrLn "Please enter the filename:"
       filename <- getLine
       putStrLn ("The file name you have entered is: " ++ filename)
       contents <- readFile filename -- read the file specified in “name” into “contents”
       lower <- (return . fn) contents
       putStrLn lower 

(我保持尽可能接近你的文字。)现在,你必须弄清楚是什么 您要申请的(String -> String)功能。这是一个纯粹的功能,应该单独开发。所以你可以写:

cwlcount :: String -> (Int, Int, Int)
cwlcount str = (length str, length (words str), length (lines str))

format :: (Int, Int, Int) -> String
format (c,w,l) = unlines $
     ["Number of characters:"
     , show c
     , "Number of words:"
     , show w
     , "Number of lines:"
     , show l
     ]

所以(format . cwlcount) :: String -> String你可以写:

 main :: IO ()
 main = commandLineUtility (format . cwlcount)

当然,这个程序有一百万个反对意见,但你可以通过零散地调查这些部分来改进它。首先,将整个字符列表放入内存并分别进行三次长度计算令人恼火。 Predude.getLine也不是非常用户友好...

目前,我们的结果看起来如此:

$ ghci Sonia_CS.hs 
GHCi, version 7.8.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
[1 of 1] Compiling Main             ( Sonia_CS.hs, interpreted )
Ok, modules loaded: Main.
>>> main
Please enter the filename:
Sonia_CS.hs
The file name you have entered is: Sonia_CS.hs
Number of characters:
816
Number of words:
110
Number of lines:
25

或更好:

$ ghc -O2 Sonia_CS.hs 
[1 of 1] Compiling Main             ( Sonia_CS.hs, Sonia_CS.o )
Linking Sonia_CS ...
$ ./Sonia_CS 
Please enter the filename:
/usr/share/dict/words
The file name you have entered is: /usr/share/dict/words
Number of characters:
2493109
Number of words:
235886
Number of lines:
235886