-2

I'm writing a movie database with Haskell. Like the title says, I am trying to give (without duplicates) the names of actors who have co-starred in at least one film with a particular actor.

Here's my code until now:

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 [])

This is my try for a function that does what I said above. It doesn't work, and I don't know why. Can anyone provide me with correction that will make the function work?

Display Name is missing
  • 6,197
  • 3
  • 34
  • 46

1 Answers1

1

As the error says,

 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'

The problem is that the function elem is defined and exported in modules Data.List and Data.Foldable. You need to qualify the occurrence of elem (or `elem`, in infix notation) to Data.List.elem, if that's the version you want.

Alternatively, you can qualify you imports instead:

import Data.List
import qualified Data.Foldable as DF

This means you can use elem from Data.List unqualified as elem, but if you want to use the Data.Foldable version, you have to use the qualified version DF.elem (or `DF.elem`, in infix notation). The downside is that you also have to qualify every other use of a Data.Foldable function.

Alternatively, you can hide elem in 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 has everything you'll need. There are also many SO questions about qualifying namespaces, for example here.

Community
  • 1
  • 1
crockeea
  • 21,651
  • 10
  • 48
  • 101
  • You can also replace `foldMap` with `concatMap` and get rid of the `Data.Foldable` import entirely. – Cirdec Mar 11 '15 at 00:39
  • 2
    @dontask You would greatly benefit from a Haskell tutorial, which you can find out more about [here](http://stackoverflow.com/a/1016986/925978). – crockeea Mar 11 '15 at 01:01
  • Another question I have is this.do u know how could I use this function to make a call with a given co-actor?im saying this because I don't know how to do it if I assign it to a variable which doesn't have anything to do with the calculation of the expression.in other words,does my code fully work ?(given what I want it to do?) – Tasos Barlas Mar 11 '15 at 02:28
  • 1
    @dontask 1. Unclear what you're asking. 2. You have to do your own homework. 3. Read a tutorial. 4. You *might*(???) be looking for a `let` statement? 5. Read a tutorial. – crockeea Mar 11 '15 at 04:06