List 如何在Haskell中从列表中筛选字符串

List 如何在Haskell中从列表中筛选字符串,list,haskell,filter,List,Haskell,Filter,我正在尝试创建一个程序,它读取一个文本文件并将文本拆分为一个列表,然后创建一个元组,其中包含每一个将在文本中出现的次数。然后我需要能够从列表中删除某些单词并打印最终列表 我尝试了不同的方法从Haskell中的字符串列表中过滤字符串,但没有成功。我发现,filter函数最适合我想做的事情,但不确定如何实现它 到目前为止,我掌握的代码是将从文件读取的文本拆分为字符串列表: toWords :: String -> [String] toWords s = words s 然后,我添加了以下内

我正在尝试创建一个程序,它读取一个文本文件并将文本拆分为一个列表,然后创建一个元组,其中包含每一个将在文本中出现的次数。然后我需要能够从列表中删除某些单词并打印最终列表

我尝试了不同的方法从Haskell中的字符串列表中过滤字符串,但没有成功。我发现,
filter
函数最适合我想做的事情,但不确定如何实现它

到目前为止,我掌握的代码是将从文件读取的文本拆分为字符串列表:

toWords :: String -> [String]
toWords s = words s
然后,我添加了以下内容以从列表中删除特定字符串:

toWords :: String -> [String]
toWords s = words s
toWords s = filter (`elem` "an")
toWords s = filter (`elem` "the")
toWords s = filter (`elem` "for")
我知道这是错误的,但我不确定怎么做。请任何人帮我做这个

以下是我到目前为止的完整代码:

main = do  
       contents <- readFile "testFile.txt"
       let lowContents = map toLower contents
       let outStr = countWords (lowContents)
       let finalStr = sortOccurrences (outStr)
       print outStr

-- Counts all the words.
countWords :: String -> [(String, Int)]
countWords fileContents = countOccurrences (toWords fileContents)

-- Splits words.
toWords :: String -> [String]
toWords s = words s
toWords s = filter (`elem` "an")
toWords s = filter (`elem` "the")
toWords s = filter (`elem` "for")

-- Counts, how often each string in the given list appears.
countOccurrences :: [String] -> [(String, Int)]
countOccurrences xs = map (\xs -> (head xs, length xs)) . group . sort $ xs

-- Sort list in order of occurrences.
sortOccurrences :: [(String, Int)] -> [(String, Int)]
sortOccurrences sort = sortBy comparing snd
main=do
内容[(字符串,Int)]
countWords fileContents=CountOccurrents(toWords fileContents)
--拆分单词。
toWords::String->[String]
toWords=单词s
toWords=过滤器(`elem`“an”)
toWords=过滤器(`elem`“the”)
toWords=过滤器(`elem`“for”)
--计数,给定列表中每个字符串出现的频率。
CountOccurrencess::[String]->[(String,Int)]
countExactures xs=map(\xs->(头xs,长度xs))。小组。排序$xs
--按出现的顺序对列表进行排序。
sortOccurrences::[(字符串,Int)]->[(字符串,Int)]
Sortoccurrencessort=通过比较snd排序

过滤器在Haskell中被称为高阶函数。你应该读一下,这类函数非常有用

也许你要找的是这样的东西:

toWords s = filter (condition) s 
filterNUmbers n = filter (>10) n
该“条件”也是一个函数,该函数必须包含要应用的筛选器

举个小例子,如果你有一个数字列表,你只想取大于10的数字,结果会是这样的:

toWords s = filter (condition) s 
filterNUmbers n = filter (>10) n

这将保留每个单词,但禁止的单词除外:

toWords s = filter (\w -> w `notElem` ["an","the","for"]) (words s)
等效变体:

-- explicit not
toWords s = filter (\w -> not (w `elem` ["an","the","for"])) (words s)
-- using and (&&) instead of elem
toWords s = filter (\w -> w/="an" && w/="the" && w/="for") (words s)
-- using where to define a custom predicate
toWords s = filter predicate (words s)
     where predicate w = w/="an" && w/="the" && w/="for") 
-- pointfree
toWords = filter (flip notElem ["an","the","for"]) . words

你能举一个你想做什么的例子吗?@thefourtheye我已经更新了我的问题,包括我正在做的事情和到目前为止我拥有的完整代码。你应该怎么做?请给出一些输入和输出示例。现在的方式是
toWords=words
中的第一行(其他行被忽略),这没有任何意义,因为这样您就可以使用
words
。顺便说一句,它甚至不会编译。
toWords
将文本文件拆分为字符串列表。所以“你好,我的名字是詹姆斯”应该是-[你好,我的名字是,詹姆斯]。谢谢,我刚刚更新了我的问题,包括我正在做的事情和完整的代码,如果这对理解我想做的事情更有帮助的话。你能告诉我单词返回了什么吗?您不应该添加到列表中?它返回一个字符串列表,因此.txt文件中的每个文本单词都将添加到列表中。谢谢,这非常有效!第一个功能运行良好,在视觉上对我来说很有意义。