displaying conetents of a list

I want to write a function to output the entire contents of a list. not sure of how to do so...This is my list i want to output type Title = String type Director = String type Year = Int type Fan = String data Film = Film Title Director Year [Fan] deriving Show -- List of films testDatabase :: [Film] testDatabase = [(Film "Casino Royale" "Martin Campbell" 2006 ["Garry", "Dave", "Zoe"]) ] ------- I know this isnt right but this is what I have so far listFilms :: [Film] -> [Film] listFilms (film@(Film title director year fans) : films) = putStrLn . show film I know to use show and putStrLn but I just don't know how to put them into the function correctly thank you. -- View this message in context: http://www.nabble.com/displaying-conetents-of-a-list-tp23470633p23470633.htm... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

2009/5/11 applebiz89
I know to use show and putStrLn but I just don't know how to put them into the function correctly
Well I hope we're not doing your homework for you but... As putStrLn is in the IO monad, listFilms should at least have a signature like this: listFilms :: [Film] -> IO () Now you know you want to call putStrLn on each item in the list. That means you want to join a bunch of functions return IO (). That sounds like a job for sequence_: listFilms films = sequence_ $ map (putStrLn.show) films Or the same thing more verbosely: listFilms [] = return () listFilms (film:films) = do putStrLn (show film) listFilms films Hope that helps, Alex

Even more succinctly:
sequence_ . map is mapM_, and putStrLn . show is print, so you get:
mapM_ print films
Dan
On Sun, May 10, 2009 at 9:59 PM, Alex MDC
2009/5/11 applebiz89
I know to use show and putStrLn but I just don't know how to put them into the function correctly
Well I hope we're not doing your homework for you but...
As putStrLn is in the IO monad, listFilms should at least have a signature like this:
listFilms :: [Film] -> IO ()
Now you know you want to call putStrLn on each item in the list. That means you want to join a bunch of functions return IO (). That sounds like a job for sequence_:
listFilms films = sequence_ $ map (putStrLn.show) films
Or the same thing more verbosely:
listFilms [] = return () listFilms (film:films) = do putStrLn (show film) listFilms films
Hope that helps, Alex
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

listFilms = [Film] -> IO ()
listFilms = putStrLn . show -- point free style
listFilms films = putStrLn $ show films
listFilms = mapM_ (putStrLn . show) -- put each movie on a separate line,
point free style
listFilms films = mapM_ (putStrLn . show) films
Hope that makes sense...
Michael
On Sun, May 10, 2009 at 5:30 PM, applebiz89
I want to write a function to output the entire contents of a list. not sure of how to do so...This is my list i want to output
type Title = String type Director = String type Year = Int type Fan = String
data Film = Film Title Director Year [Fan] deriving Show
-- List of films
testDatabase :: [Film] testDatabase = [(Film "Casino Royale" "Martin Campbell" 2006 ["Garry", "Dave", "Zoe"]) ]
-------
I know this isnt right but this is what I have so far
listFilms :: [Film] -> [Film] listFilms (film@(Film title director year fans) : films) = putStrLn . show film
I know to use show and putStrLn but I just don't know how to put them into the function correctly
thank you. -- View this message in context: http://www.nabble.com/displaying-conetents-of-a-list-tp23470633p23470633.htm... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (4)
-
Alex MDC
-
applebiz89
-
Daniel Peebles
-
Michael Snoyman