Haskell逻辑输出错误

时间:2015-03-14 23:46:50

标签: haskell

以下是我的以下代码,用于过滤掉包含以下用户名的电影是我的数据库

 testDatabase :: [Film]
testDatabase = [
                    ("Casino Royale", ["Daniel Craig", "Eva Green", "Judi Dench"], 2006, ["Garry", "Dave", "Zoe", "Kevin", "Emma"]),
                ("The Departed", ["Leonardo DiCaprio", "Matt Damon", "Jack Nicholson"], 2006, ["Zoe", "Emma", "Paula", "Olga", "Dave"]),
                ("Inception", ["Leonardo DiCaprio"], 2010, ["Chris", "Emma", "Jo", "Bill", "Dave", "Liz", "Wally", "Zoe", "Amy", "Sam", "Paula", "Kevin", "Olga"]),
                ("Up in the Air", ["George Clooney", "Vera Farmiga"], 2009, ["Wally", "Liz", "Kevin", "Tim", "Emma"]),
                ("The Shawshank Redemption", ["Tim Robbins", "Morgan Freeman"], 1994, ["Jo", "Wally", "Liz", "Tim", "Sam", "Zoe", "Emma", "Garry", "Olga", "Kevin"])
           ]

这里有如上所述的功能

showFanFilms :: String -> [Film] -> [Film]
showFanFilms name database
 =  filter(\(title, cast, year, fan:fans) -> fan == name) database

每次我使用正确的输入运行代码时,例如

showFanFilms "Tim" testDatabase

它一直给我一个空数组

   []

任何想法为什么?

1 个答案:

答案 0 :(得分:1)

当您使用模式fan:fans时,fan只会绑定到粉丝列表中的第一个元素。由于"Tim"永远不会出现在那个地方,所以没有点击。

你可能想要像

这样的东西
showFanFilms name database
 =  filter(\(title, cast, year, fans) -> name `elem` fans) database

代替。