[Haskell-cafe] IO Help

2008-05-08 Thread Mark Wallsgrove

Hey,

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
Smoky
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] IO Help

2008-05-08 Thread Mark Wallsgrove

Thank you very much for your fast response!

Ok, that is now changed, but everything else in my program is expecting 
Catalogue without IO. Is there a way to change IO Catalogue into Catalogue?


Henning Thielemann wrote:


On Thu, 8 May 2008, Mark Wallsgrove wrote:

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)


type should be

  loadData :: String - IO Catalogue


But using plainly 'read' may not satisfy you, because the program will 
abort unrecoverably if the file has corrupt content. You may want to use 
'reads' and check manually if parsing was successful:


case reads x of
   [(cat, )] - return cat
   _ - fail corrupt file content



___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] IO Help

2008-05-08 Thread Mark Wallsgrove

Was there? I have been google'ing that problem for ages..

Just one more thing. I have to make a menu system where the user chooses 
what functionality they want. Because you cannot change a value once it 
is set I have used recursion so that when something changes it then 
calls the menu back up. I feel this is way to memory consuming. Is there 
another way?


CODE:

main catalogue playlist:
 x = choose option
 passtofunction x


passtofunction value:
 function1 1 = main (functionName value) playlist
 function2 2 = main catalogue (functionName value)

Henning Thielemann wrote:


On Thu, 8 May 2008, Mark Wallsgrove wrote:


Thank you very much for your fast response!

Ok, that is now changed, but everything else in my program is 
expecting Catalogue without IO. Is there a way to change IO Catalogue 
into Catalogue?


Btw. there was a nice article precisely about the issue How to get rid 
of the IO? in the old Hawiki. What is the progress in bringing back 
Hawiki?





___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] IO Help

2008-05-08 Thread Mark Wallsgrove

Thank you all for your help, you have been invaluable

Thomas Davie wrote:


On 8 May 2008, at 16:31, Mark Wallsgrove wrote:


Was there? I have been google'ing that problem for ages..

Just one more thing. I have to make a menu system where the user 
chooses what functionality they want. Because you cannot change a 
value once it is set I have used recursion so that when something 
changes it then calls the menu back up. I feel this is way to memory 
consuming. Is there another way?


While this method feels like it should consume lots of memory, it in 
fact doesn't.  Remember that you're dealing with a graph machine, and 
that no stack is maintained of all the calls you've made.  The garbage 
collector will simply follow you through the menu system clearing up the 
memory behind you.


Bob



___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe