[Haskell-cafe] IO Help

Mark Wallsgrove mark at simplycompute.co.uk
Thu May 8 08:45:42 EDT 2008


I am studying Haskell as a unit at University. I find the concept and design
idea's of Haskell interesting but I am finding my self struggling to
understand the relationship between the normal state and IO state of
Haskell. This is my situation:

I have created 12 functions for a program which take in two types:

type Catalogue = [Track]
type Playlist = [Track]

The definition for track is as follows:

-- Track = ArtistDetails Title Length PCount
data Track = Track ArtistType String Float Int
      deriving (Show,Eq,Read)

-- Popular = Artist | Composor Performer
data ArtistType = Popular String | Classical String String
      deriving (Show,Eq,Read)

I have managed to save the data to a file using this code:

--Saving Data
saveData :: String -> Catalogue -> IO()
saveData fileName catalogue = writeFile fileName (show catalogue)

Problem now is reading the data back into the program. When I read the data
back into the program it comes as IO [Track]. This is the code I have been
using to load the data:

loadData :: String -> Catalogue
loadData fileName = do x <- readFile fileName
                        return (read x :: Catalogue)

I think I have missed a trick some where or I am using IO wrong, I really
don't know. I believe it is the latter. I have been told that my definition
for the function is wrong and that I should use the lazy approach to fix the
problem. But this still leaves me with IO [Track].

Could someone inform me on what I am doing wrong?

Thank you in advance

More information about the Haskell-Cafe mailing list