我正在關注這本書。 http://book.realworldhaskell.org/read/getting-started.html
我完全被困在如何計算檔案中的單詞數上。在我試圖解決這個問題的程序中,我還注意到一些有趣的事情。
main = interact wordCount
where wordCount input = show (length (input)) "\n"
我注意到如果沒有“\n”字符,我會在數字末尾附加一個百分比符號。
main = interact wordCount
where wordCount input = show (length (input))
所以我有 2 個問題,如果我不附加“\n”字符,為什么會得到百分比符號,以及如何計算檔案中的所有單詞?這比我學過的任何解釋性語言都要復雜得多。但我喜歡挑戰。
在我的文本檔案中,我洗掉了除一個之外的所有城市。下面是我的txt檔案的內容
Teignmouth, England
uj5u.com熱心網友回復:
您的 shell 實際上附加了 a
%因為您的程式的輸出沒有以換行符結尾(請參閱此處)。POSIX 標準將“行”定義為以 a 結尾的內容\n(請參閱此處)。該功能
words是您正在尋找的功能:
main = interact wordCount
where wordCount input = (show $ length $ words input) "\n"
請注意,$運算子允許減少括號。此代碼等效:
main = interact wordCount
where wordCount input = (show (length (words input))) "\n"
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/374171.html
標籤:哈斯克尔
