2014-12-06 23 views
-2

我對Haskell來說相當新。我一直在試圖創建一個實用程序來計算幾天內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 

我曾嘗試使用「砍」,發現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 

任何幫助將非常感激。

+0

那麼到底是什麼問題了嗎?你所發佈的代碼在格式上沒有正確的格式,我首先會建議解決這個問題,但你幾乎只描述了你想要做的事情,而不是你遇到的問題。 – bheklilr 2014-12-06 17:14:24

+0

對不起,新的堆棧溢出,它沒有正確地複製代碼。由於我想返回文件中的行數和單詞數,所以我遇到了計數函數的問題。我現在已經能夠解決這個問題。 – user610 2014-12-06 17:19:28

+0

我遇到的新問題是,ReadFile沒有返回正確的行數。例如,如果我給它的文件「new.txt」有4行 - 它返回4然而,當我通過控制檯餵它時,它返回1 :(任何幫助?謝謝 – user610 2014-12-06 17:22:38

回答

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 
+0

非常感謝Arthur。我設法想出了一個解決方案,現在使用countLines和countWords使用這種格式str =(show(length(lines str))),這似乎也很好。 – user610 2014-12-06 20:08:22

+0

對,我正在將它們聚集的結果格式分開,因爲我將IO與純粹的計算分開。當然,他們都可以放在一起。 – Michael 2014-12-06 20:10:59

+0

是的,我更喜歡你的方法,因爲它看起來更乾淨簡潔!謝謝!! – user610 2014-12-06 20:12:04