我想使用交互作用,它返回最常用的單詞和它出現的時間#的Haskell程序。我已經看到了排序的例子 - 但我不需要知道所有單詞的計數,我只需要最重複的單詞。到目前爲止,我有:哈斯克爾 - 計數列表中最重複的元素與交互
import Data.List -- (sort)
import Data.Char -- (isAlpha, toLower)
import Data.Ord -- (maximumBy)
main =
interact
$ unwords
-- comment: here show the size of the list and the word (probably head)
. maximumBy(comparing length)
. group
. sort
. words
. map (\char -> if isAlpha char then toLower char else ' ')
以上編譯。 maximumBy
給出了最常用的詞是這樣的:
[the, the, the, the, the, the, the, the...]
爲"the"
出現在文本字的次數;我已經驗證"the"
是我提供的文本中最常用的詞。
我想輸出是這樣的:"the, 318"
我試過只給第一個字母「t」和3如下:
import Data.List -- sort
import Data.Char -- isAlpha, toLower
import Data.Ord -- maximumBy
main =
interact
$ unwords
. map (\(n, w) -> show n ++ ", " ++ show w)
. map (\s -> (length s, head s))
. maximumBy(comparing length)
. group
. sort
. words
. map (\char -> if isAlpha char then toLower char else ' ')
這給輸出:
"3, 't' 3, 't' 3, 't' 3, 't' ..."
任何人都知道我在做什麼錯了?
感謝您的幫助。這是我想要的解決方案。 – Zorkolot 2014-11-06 11:48:20