过滤 Haskell 中的元组列表
Filter a list of tuples in Haskell
我是 Haskell 的新手,我正在尝试过滤包含一个字符串和一个字符串列表的元组列表。我想根据字符串列表是否包含某个字符串进行过滤。我想创建一个列表列表,其中每个列表都是每个元组的第一个元素,该元素对过滤器的计算结果为真。
示例:
给出这个元组列表
编程语言=
[ ("CptS121" , ["C"]),
("CptS122", ["C++"]),
("CptS223", ["C++"]),
("CptS233", ["Java"]),
("CptS321", ["C#","Java"]),
("CptS322", ["Python","Java脚本"]),
("CptS355", ["Haskell", "Python", "PostScript", "Java"]),
("CptS360", ["C"]),
("CptS370", ["Java"]),
("CptS315", ["Python"]),
("CptS411", ["C", "C++"]),
("CptS451", ["Python", "C#", "SQL","Java"]),
("CptS475", ["Python", "R"])
]
我想过滤包含 ["Python","C","C++"] 的元组并创建一个列表列表,其中每个子列表是使用每种语言的课程。
因此,[[Python","C","C++"] 将输出 [ ["CptS322","CptS355","CptS315","CptS451","CptS475"], [ "CptS121","CptS360","CptS411"], ["CptS122","CptS223","CptS411"]]
到目前为止我有以下内容:
filter_courses [] courses = []
filter_courses list [] = []
filter_courses list courses = map snd (filter ((`elem` courses).fst) list)
这会过滤 progLanguages 并输出使用 'courses' 中任何列出的语言的所有元组的列表。
根据上面列表的输出,我正在尝试执行以下操作...
filter_more [] langs = []
filter_more list [] = []
filter_more list langs = map fst (filter ((`elem` langs).snd) list)
这会输出使用任何语言的所有课程的列表,而不是每种语言的课程列表。
下面的怎么样:
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"]]
我将任务分为两个步骤:首先,我创建了一个函数 coursesUsing
,列出了 给定 一种语言的课程。然后我使用 map
.
将该函数应用于每种语言
或者,使用列表理解:
filterCourses langs courses = [[course | (course, langs) <- courses, lang `elem` langs] | lang <- langs]
我是 Haskell 的新手,我正在尝试过滤包含一个字符串和一个字符串列表的元组列表。我想根据字符串列表是否包含某个字符串进行过滤。我想创建一个列表列表,其中每个列表都是每个元组的第一个元素,该元素对过滤器的计算结果为真。
示例:
给出这个元组列表
编程语言=
[ ("CptS121" , ["C"]),
("CptS122", ["C++"]),
("CptS223", ["C++"]),
("CptS233", ["Java"]),
("CptS321", ["C#","Java"]),
("CptS322", ["Python","Java脚本"]),
("CptS355", ["Haskell", "Python", "PostScript", "Java"]),
("CptS360", ["C"]),
("CptS370", ["Java"]),
("CptS315", ["Python"]),
("CptS411", ["C", "C++"]),
("CptS451", ["Python", "C#", "SQL","Java"]),
("CptS475", ["Python", "R"])
]
我想过滤包含 ["Python","C","C++"] 的元组并创建一个列表列表,其中每个子列表是使用每种语言的课程。
因此,[[Python","C","C++"] 将输出 [ ["CptS322","CptS355","CptS315","CptS451","CptS475"], [ "CptS121","CptS360","CptS411"], ["CptS122","CptS223","CptS411"]]
到目前为止我有以下内容:
filter_courses [] courses = []
filter_courses list [] = []
filter_courses list courses = map snd (filter ((`elem` courses).fst) list)
这会过滤 progLanguages 并输出使用 'courses' 中任何列出的语言的所有元组的列表。
根据上面列表的输出,我正在尝试执行以下操作...
filter_more [] langs = []
filter_more list [] = []
filter_more list langs = map fst (filter ((`elem` langs).snd) list)
这会输出使用任何语言的所有课程的列表,而不是每种语言的课程列表。
下面的怎么样:
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"]]
我将任务分为两个步骤:首先,我创建了一个函数 coursesUsing
,列出了 给定 一种语言的课程。然后我使用 map
.
或者,使用列表理解:
filterCourses langs courses = [[course | (course, langs) <- courses, lang `elem` langs] | lang <- langs]