简体   繁体   English

过滤 Haskell 中的元组列表

[英]Filter a list of tuples in Haskell

I'm pretty new to Haskell and I'm trying to filter a list of tuples that contain a string and a list of strings.我是 Haskell 的新手,我正在尝试过滤包含一个字符串和一个字符串列表的元组列表。 I want to filter based on if the list of strings contains a certain string.我想根据字符串列表是否包含某个字符串进行过滤。 I'd like to create a list of lists where each list is the first element of every tuple that evaluates to true for the filter.我想创建一个列表列表,其中每个列表都是每个元组的第一个元素,该元素对过滤器的计算结果为真。

Example:例子:
Given this list of tuples给定这个元组列表
progLanguages =程序语言 =
[ ("CptS121", ["C"]), [(“CptS121”,[“C”]),
("CptS122", ["C++"]), ("CptS122", ["C++"]),
("CptS223", ["C++"]), ("CptS223", ["C++"]),
("CptS233", ["Java"]), ("CptS233", ["Java"]),
("CptS321", ["C#","Java"]), ("CptS321", ["C#","Java"]),
("CptS322", ["Python","JavaScript"]), ("CptS322", ["Python","JavaScript"]),
("CptS355", ["Haskell", "Python", "PostScript", "Java"]), ("CptS355", ["Haskell", "Python", "PostScript", "Java"]),
("CptS360", ["C"]), ("CptS360", ["C"]),
("CptS370", ["Java"]), ("CptS370", ["Java"]),
("CptS315", ["Python"]), ("CptS315", ["Python"]),
("CptS411", ["C", "C++"]), ("CptS411", ["C", "C++"]),
("CptS451", ["Python", "C#", "SQL","Java"]), ("CptS451", ["Python", "C#", "SQL","Java"]),
("CptS475", ["Python", "R"]) (“CptS475”,[“Python”,“R”])
] ]

I want to filter the tuples that contain ["Python","C","C++"] and create a list of lists where each sub list is the courses that uses each language.我想过滤包含 ["Python","C","C++"] 的元组并创建一个列表列表,其中每个子列表是使用每种语言的课程。

So, ["Python","C","C++"] would output [ ["CptS322","CptS355","CptS315","CptS451","CptS475"], ["CptS121","CptS360","CptS411"], ["CptS122","CptS223","CptS411"] ]所以,["Python","C","C++"] 会 output [["CptS322","CptS355","CptS315","CptS451","CptS475"], ["CptS121","CptS360"," CptS411"], ["CptS122","CptS223","CptS411"] ]

So far I have the following:到目前为止,我有以下内容:

    filter_courses [] courses = []
    filter_courses list [] = []
    filter_courses list courses = map snd (filter ((`elem` courses).fst) list)

This filters progLanguages and outputs a list of all the tuples that use any of the listed languages in 'courses'.这会过滤 progLanguages 并输出使用“课程”中任何列出的语言的所有元组的列表。

Taking the output from the above list, im trying to do the following...从上面的列表中取出 output,我正在尝试执行以下操作......

    filter_more [] langs = []
    filter_more list [] = []
    filter_more list langs = map fst (filter ((`elem` langs).snd) list)

This outputs a list of all the courses that use any of the languages instead of a list of courses for each language.这将输出使用任何语言的所有课程的列表,而不是每种语言的课程列表。

How about the following:以下情况如何:

progLanguages = [ ("CptS121", ["C"])
                , ("CptS122", ["C++"])
                , ("CptS223", ["C++"])
                , ("CptS233", ["Java"])
                , ("CptS321", ["C#","Java"])
                , ("CptS322", ["Python","JavaScript"])
                , ("CptS355", ["Haskell", "Python", "PostScript", "Java"])
                , ("CptS360", ["C"])
                , ("CptS370", ["Java"])
                , ("CptS315", ["Python"])
                , ("CptS411", ["C", "C++"])
                , ("CptS451", ["Python", "C#", "SQL","Java"])
                , ("CptS475", ["Python", "R"])
                ]

filterCourses :: Eq a => [a] -> [(b, [a])] -> [[b]]
filterCourses langs courses = map coursesUsing langs
  where coursesUsing lang = map fst . filter (elem lang . snd) $ courses

main = do
  print $ filterCourses [] progLanguages
-- []
  print $ filterCourses ["Python","C","C++"] progLanguages
-- [["CptS322","CptS355","CptS315","CptS451","CptS475"],["CptS121","CptS360","CptS411"],["CptS122","CptS223","CptS411"]]

I split the task into two steps: first, I made a function coursesUsing that lists the courses given a language.我将任务分为两个步骤:首先,我制作了一个 function coursesUsing ,它列出了给定语言的课程。 Then I applied the function to each language using map .然后我使用map将 function 应用于每种语言。

Or, using list comprehension:或者,使用列表理解:

filterCourses langs courses = [[course | (course, langs) <- courses, lang `elem` langs] | lang <- langs]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM