给予(没有重复)与特定演员共同出演至少一部电影的演员的名字

时间:2015-03-10 22:50:38

标签: haskell haskell-platform template-haskell

我正在用Haskell写一个电影数据库。就像标题所说的那样,我试图给予(没有重复)演员的名字,这些演员曾与一位特定的演员共同出演至少一部电影。

这是我的代码,直到现在:

import Text.Printf
import Data.List
import Data.Ord
import Data.Foldable 

type Title = String
type Actor = String
type Cast = [Actor]
type Year = Int
type Fan = String
type Fans = [Fan]
type Period = (Year, Year)

data Film = Film Title Cast Year Fans
deriving(Show,Read,Eq)




testDatabase :: [Film]
testDatabase = [(Film "Casino Royale" ["Daniel Craig", "Eva Green", "Judi Dench"] 2006 ["Garry", "Dave", "Zoe", "Kevin", "Emma"]),       
            (Film "Cowboys & Aliens" ["Harrison Ford", "Daniel Craig", "Olivia Wilde"] 2011 ["Bill", "Jo", "Garry", "Kevin", "Olga", "Liz"]),         
            (Film "Catch Me If You Can" ["Leonardo DiCaprio", "Tom Hanks"] 2002 ["Zoe", "Heidi", "Jo", "Emma", "Liz", "Sam", "Olga", "Kevin", "Tim"]),
            (Film "Mamma Mia!" ["Meryl Streep", "Pierce Brosnan", "Colin Firth"] 2008 ["Kevin", "Jo", "Liz", "Amy", "Sam", "Zoe"]),
            (Film "Titanic" ["Leonardo DiCaprio", "Kate Winslet"] 1997 ["Zoe", "Amy", "Heidi", "Jo", "Megan", "Olga"]),
            (Film "Quantum of Solace" ["Daniel Craig", "Judi Dench"] 2008 ["Bill", "Olga", "Tim", "Zoe", "Paula"])]

coactors :: Actor -> [Film] -> [Actor]  
coactors [] = []
coactors a -> filter (/= a) . nub . concatMap (\(Film _ c _ _) -> if a     `elem` c then c else [])

这是我尝试执行上述操作的功能。它不起作用,我不知道为什么。任何人都可以为我提供纠正功能吗?

1 个答案:

答案 0 :(得分:1)

正如错误所说,

 Ambiguous occurrence `elem'
 It could refer to either `Data.List.elem',
                          imported from `Data.List' 
                          (and originally defined in `GHC.List')
                       or `Data.Foldable.elem',
                          imported from `Data.Foldable'

问题是函数elem是在模块Data.ListData.Foldable中定义和导出的。您需要将elem(或`elem`,以中缀表示法)的出现限定为Data.List.elem,如果这是您想要的版本。

或者,您可以取代导入资格:

import Data.List
import qualified Data.Foldable as DF

这意味着您可以使用elem Data.List取消限定为elem,但如果您想使用Data.Foldable版本,则必须使用限定版DF.elem 1}}(或`DF.elem`,以中缀表示法)。缺点是您还必须使用Data.Foldable函数的其他所有用途。

或者,您可以隐藏elem中的Data.Foldable

import Data.List -- import all of Data.List unqualified
import Data.Foldable hiding (elem) -- import all of Data.Foldable unqualified, but hide `elem`.

This link拥有您需要的一切。关于限定命名空间的问题也很多,例如here