从List Haskell过滤字符串

时间:2015-05-08 16:15:19

标签: string list haskell

我正在尝试编写一个读取文本文件的程序,然后显示文件中的单词的频率和计数。我接下来需要做的是从文本文件中过滤某些单词。我一直在寻找在线资源几个小时,但仍然无法找到我想要的东西!

到目前为止,我已经提供了该程序的代码:

lowercase = map toLower
top doc = wordPairs
    where
        listOfWords = words (lowercase doc)
        wordGroups  = group (sort listOfWords)
        wordPairs   = reverse
                    $ sort
                    $ map (\x -> (length x, head x))
                    $ filterWords
                    wordGroups

filterWords :: String -> String
filterWords = filter (all (`elem` ["poultry outwits ants"])) . words

2 个答案:

答案 0 :(得分:1)

如果以不同的方式拆分程序可能会更容易。例如

import Data.List(group,sort)
import Control.Arrow((&&&))

freq :: Ord a => [a] -> [(Int,a)]
freq = reverse . sort . map (length &&& head) . group . sort

第二部分将定义此函数的输入。您只想过滤某些元素。

select :: Eq a => [a] -> [a] -> [a]
select list = filter (`elem` list)

这些将使测试更容易,因为您不需要特定的类型输入。

最后,你可以把它们绑在一起

freq $ select ["a","b","c"] $ words "a b a d e a b b b c d e c"

会给你

[(4,"b"),(3,"a"),(2,"c")]

答案 1 :(得分:0)

我的代码可以解决您的问题

top :: String -> [(Int,String)] --Signature is always important
top = sorter . wordFrequency . groups . filtered --just compose `where` functions
    where
        -- This will filter your words
        filtered = filter (`notElem` ["poultry","outwits","ants"]) . words . map toLower 
        -- Group your words
        groups = group . sort 
        -- Create the pairs of (count, word)
        wordFrequency = map (length &&& head)
        -- Sort your list by first. for reverse just switch a and b
        sorter = sortBy (\ a b -> fst b `compare` fst a)