Wednesday 15 April 2015

Haskell: converting data from file to Data type -


I am having trouble converting a list of strings from a text file to a data type, Made a movie called . I will display the following code:

Inside the movies

  "Casino Royale" "Daniel Craig", "Eva Green", "Judy Dench" 2006 "Gary", "Dave", "Joe", "Kevin", "Emma" "Cowboy and Aliens" "Harrison Ford", "Daniel Craig", "Olivia Wilde" 2011 "Bill", "Jo "Gary", "Kevin", "Olga", "Liz" "Catch Me Is You Can" "Leonardo DiCaprio", "Tom Hanks" 2002 "Zoe", "Heidi", "Joe", "Emma" / P> 
  type title = string type actor = string processing (Cast, Year, Fan) Main :: IO () Key = What putStr "What is your name?" : "Name and lieutenant; GetLine firstdatabase & lt; - readFile "movies.txt" putStr firstdatabase database = First read database: [movie] name of mainLoop database  

What to do?

It can be completed very easily with pars:

 < Code> import text Parasak (parsec, parse error, pars - between type and parser, noneOf, sepby, multiple 1 - coordinator, four, spaces, digits, new line - simple parser) - Parsing a string for string string: Parsec string string string string between = (four '' '' '(four' '') $ many1 $ noneOf "\" \ N "- Parsing a string in the list of string listsofsets :: Parsek string u [string] listoffstring = String lit`s B '(four', '> spaces) - Paint a string for an int intLit :: Parsec string, read int intLit = fmap $ many1 digit - or `read & lt; $ & gt; Many points with control 1. Application Film: Parsek String U film film = Doo - Optionally titled `title & lt; - stringLit & lt; * with control * newline` .applicative title & lt; - string light newline cast & Lt; - listOfStrings newline year & lt; new line fans & list - listoffsprings newline returns (Title, artist, year, fans) - Alternatively, you can define it in the same time (with control. Applicative) - movie = (,) - & lt; $ & Gt; String lt & lieutenant; * Newline - & lt; * & Gt; ListOfStrings & lt; * Newline - & lt; * & Gt; IntLit & lt; * Newline - & lt; * & Gt; Listoffstring and lieutenant; * Newline - It looks very much like your real file films: Parsek String You [film] movies = movie 'SEPBI' Newline  

then you can use it

loadDB :: file path -> Io (either Parsair [film]) load db file name = db < - readFile filename returns $ parsing movies "movies" db Main :: IO () main = do puttr "What is your name?" Name & lt; - getLine db '& lt; - loadDB "movies.txt" case db left error - & gt; DoStrLn "Error loading the movie database:" PrintRright - db - & gt; MainLoop DB name

No comments:

Post a Comment