2015-12-16 25 views
0

當我嘗試計算文件的單詞時,Haskell出現問題。我只是一個初學者,這是我的第一個程序,所以我很確定這是一個非常簡單的錯誤。 我正在使用擁抱來運行我的代碼。直到現在,我學會了如何從文件中讀取數據,但是我沒有統計數據中的文字。我的代碼是這樣的haskell文件的字數

main = do { 
contents <- readFile "/tmp/foo.txt"; 
let contents2 = replace"."""contents; 
let contents3 = replace"!"""contents2; 
let lower = map toLower contents3; 
let chop = words(lower); 
let count = show(length chop)++"\n"; 
putStrln $"This file has"++count++"words"; 
} 

任何幫助將不勝感激。謝謝!

+3

'READFILE 「文件」 >> =打印。長度 。詞「,但你應該學習一些haskell之前(遵循一些教程http://learnyouahaskell.com/ – josejuan

+0

它的工作完美,我將遵循教程。非常感謝你:) – user3104760

回答

0

您可以使用下列之一:

main = readFile "/tmp/foo.txt" >>= print . length . words 

main = do 
    contents <- readFile "/tmp/foo.txt" 
    print . length . words $ contents 
+0

所以當我們嘗試使用相同的邏輯找到任何字符?有沒有任何捷徑的方法? – user3104760

+0

我嘗試了這樣的代碼,但給了字母「s」的錯誤。錯誤的原因是什麼? readFile「/tmp/foo.txt">>= print。長度 。過濾器s; – user3104760

+0

你正在尋找一個特定字符的總數嗎?您可以使用'readFile「/tmp/foo.txt」>> = print。長度 。過濾器(=='s')'。 – nobody