-2

I am trying to read a file. The Title is read correctly, but when I try to read the Cast (actors) it doesn't seem to stop and search for the year. is there a mistake in parsing Actors or parsing Chars?

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

-- Parsing methods for reading the file

parseLines :: [String] -> Film
parseLines list = (list !! 0, parseActors (drop 1 list), read(list !! 2), parseActors (drop 3 list))

parseFans :: [String] -> Fans
parseFans [] = []
parseFans list
    | length list > 0 = [(list !! 0)] ++ parseFans (drop 1 list)

parseActors :: [String] -> Cast
parseActors [] = []
parseActors list
    | length list > 0 = [(list !! 0)] ++ parseActors (drop 1 list)

parseChars :: String -> String -> [String]
parseChars [] _ = []
parseChars (x:xs) stringCount
    | x == ',' = [stringCount] ++ parseChars xs ""
    | otherwise = (parseChars xs (stringCount ++ [x]))

parseAll :: [String] -> [Film]
parseAll [] = []
parseAll (x:xs) = parseLines (parseChars x "") : (parseAll xs)

and i get this error

|The Gunman| |Idris Elba,Sean Penn,Javier Bardem,2015,Garry,Dave,Zoe,Kevin,| |*Main> *** Exception: Prelude.read: no parse

this is my text file

    The Gunman,Idris Elba,Sean Penn,Javier Bardem,2015,Garry,Dave,Zoe,Kevin,Emma, 
    The Shawshank Redemption, Tim Robbins, Morgan Freeman, Bob Gunton, 1994, Bill, Jo, Garry, Kevin, Olga, Liz, 
    The Dark Knight, Christian Bale, Heath Ledger,Aaron Eckhart, 2008, Zoe, Heidi, Jo, Emma, Liz, Sam, Olga, Kevin, Tim, 
    Inception, Leonardo DiCaprio, Ellen Page, 2010, Jo, Emma, Zack, Olga, Kevin,

1 Answer 1

1
parseActors :: [String] -> Cast
parseActors [] = []
parseActors list
    | length list > 0 = [(list !! 0)] ++ parseActors (drop 1 list)

Have you tried this out to see what it does?

ghci> parseActors ["one", "two", "three"]
["one", "two", "three"]

What you have done is written an overly complicated version of:

parseActors :: [String] -> Cast
parseActors list = list

Likewise,

parseFans :: [String] -> Fans
parseFans [] = []
parseFans list
    | length list > 0 = [(list !! 0)] ++ parseFans (drop 1 list)

This code is identical to parseActors, you've just changed the name to parseFans and the result type to Fans. This behaves exactly the same as:

parseFans :: [String] -> Fans
parseFans list = list

I'm going to just go ahead and ignore the above for now. Let's take parseChars for a spin instead.

ghci> parseChars "one,two,three," ""
["one", "two", "three"]

Ah, a function that actually does parsing. I'm not sure how you expect this one to work, but I presume it is working as expected.

I want to draw your attention to this part of parseLines:

read(list !! 2)

This is what you are using to try and extract the year. And this is what is throwing the error: Prelude.read: no parse. Note how it tells you that a use of Prelude.read is the culprit.

Okay. Let's take the first film and run it through some of this code.

ghci> let gunman = "The Gunman,Idris Elba,Sean Penn,Javier Bardem,2015,Garry,Dave,Zoe,Kevin,Emma,"
ghci> let list = parseChars gunman ""
ghci> list
["The Gunman","Idris Elba","Sean Penn","Javier Bardem","2015","Garry","Dave","Zoe","Kevin","Emma"]
ghci> list !! 2
"Sean Penn"

Notice how list !! 2 is not the year. There's your problem. Well, one of them, anyways.

So lessons hopefully learned:

  • Use the interactive prompt (ghci) to test out pieces of your code and make sure they behave the way you expect.
  • Scrutinize error messages, they often can give you clues about what's wrong with your code.
1
  • i have tried the first one and i have the same result. i realize that i get the error because the Cast list does not stop getting actors.. and when it reaches the end of the line the year cant be found especially in the position list !! 2. now i need to create a function that search for actor names and stops when it finds a year or something?
    – Onix
    Mar 17, 2015 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.