如何从haskell中的列表中查找常用单词

时间:2017-12-19 21:54:10

标签: haskell functional-programming

嗨,我对Haskell编程有点新意。我只是想知道如何从Haskell的列表中找到10个常用词。

列表示例:

["there", "is", "is", "a", "man", ...]

使用示例:

getCommonWords xs = count(10, xs)

如何从列表中删除10个常用词:

removeCommonWords xs = drop(10, xs)

任何帮助或正确的方向都会非常感谢。

1 个答案:

答案 0 :(得分:2)

这可能会让你开始

commonwords n = snd . unzip . take n . reverse . sort . map pair . group . sort
     where pair x = (length x, head x)

过滤掉列入黑名单的字词

filterBlackList = filter (`notElem` blacklist)

你需要结合两个......

这是连接两个函数的一种简单方法

removecommon :: Ord a => Int -> [a] -> [a]
removecommon n ws = filter (`notElem` commonwords n ws) ws

例如

> let ws = words "she sells seashells by the seashore the shells she sells are surely seashells so if she sells shells on the seashore I'm sure she sells seashore shells"

> removecommon 6  ws

回馈

["by","are","surely","so","if","on","I'm","sure"]