Haskell使用折叠功能实现过滤器列表

问题描述

filterList :: (Eq a) => a -> [(a,b)] -> [(a,b)] 

>filterList " foo " [( " foo ",1),( " bar ",2),( " foo ",3)]
[( " foo ",3)]

我想出了两种方法来解决这个问题,

`first way with list comprehension :` filterList a ((x,y):xs) =[(b,c)|(b,c)<-((x,y):xs),a==b]



  second way with recursive function: 
 filterList2 a []=[]
 filterList2 a ((x,y):xs)|a==x = (x,y):filterList2 a xs
                         |otherwise = filterList2 a xs

但是我想用文件夹功能解决它,我被卡住了。

filterList a ((x,y):xs) = foldr filter1 a ((x,y):xs)
                          
filter1 b ((x,y):xs)|b==x = (x,y)
                    |otherwise = filter1 b xs

这不起作用。

非常感谢您的帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)