[Haskell-cafe] IO help

2009-05-07 Thread applebiz89

I havent done much IO at all in haskell, only within the function itself.
However I want to get the input from the interface for the function and
havent done this before.

In my main function, I want to ask the user what they would like to do
'become fan' for example, then with their choice initiate the function and
ask for the appropriate input for that function. This is the code below:

main :: IO()
do putStr Hi there! what is your name: 
fanName = getLine
do putStr 1 = Insert film, 2 = Become a Fan, 3 = The number of fans of a
film, 4 = Film released in a year: 
input = getLine
read input :: Int
(if input == 1 then main x = insertFilm [] else if input == 2 then main x =
becomeFan [] else if input == 3 then main x = numberOfFans [])

Say they choose film in a given year function function:

filmsInGivenYear :: Int - [Film] - [String]
filmsInGivenYear filmYear films = [ title | (Film title director year fans)
- films, year == filmYear]

I need to ask the user what filmYear they want to insert. But i need to do
this from the main function...I chose to do an if statement to choose what
function they want, but i dont know where to go from there?

any help will be greatly appreciated.

thanks

apple
-- 
View this message in context: 
http://www.nabble.com/IO-help-tp23423403p23423403.html
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


Re: [Haskell-cafe] IO help

2009-05-07 Thread Miguel Mitrofanov

I have a suggestion.

Why don't you grab some introductory book on Haskell and learn Haskell syntax a 
little?

applebiz89 wrote on 07.05.2009 13:46:

I havent done much IO at all in haskell, only within the function itself.
However I want to get the input from the interface for the function and
havent done this before.

In my main function, I want to ask the user what they would like to do
'become fan' for example, then with their choice initiate the function and
ask for the appropriate input for that function. This is the code below:

main :: IO()
do putStr Hi there! what is your name: 
fanName = getLine
do putStr 1 = Insert film, 2 = Become a Fan, 3 = The number of fans of a
film, 4 = Film released in a year: 
input = getLine



read input :: Int

What exactly do you think this line is going to do?



(if input == 1 then main x = insertFilm [] else if input == 2 then main x =
becomeFan [] else if input == 3 then main x = numberOfFans [])

All these main x = are just syntax errors.




Say they choose film in a given year function function:

filmsInGivenYear :: Int - [Film] - [String]
filmsInGivenYear filmYear films = [ title | (Film title director year fans)
- films, year == filmYear]

I need to ask the user what filmYear they want to insert. But i need to do
this from the main function...I chose to do an if statement to choose what
function they want, but i dont know where to go from there?

any help will be greatly appreciated.

thanks

apple

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


Re: [Haskell-cafe] IO help

2009-05-07 Thread Bulat Ziganshin
Hello applebiz89,

Thursday, May 7, 2009, 1:46:34 PM, you wrote:

 main :: IO()

you may find http://haskell.org/haskellwiki/IO_inside interesting


-- 
Best regards,
 Bulatmailto:bulat.zigans...@gmail.com

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


Re: [Haskell-cafe] IO help

2009-05-07 Thread Adrian Neumann

Have a look at the wikibook:

http://en.wikibooks.org/wiki/Haskell/Simple_input_and_output


Am 07.05.2009 um 11:46 schrieb applebiz89:



I havent done much IO at all in haskell, only within the function  
itself.
However I want to get the input from the interface for the function  
and

havent done this before.

In my main function, I want to ask the user what they would like to do
'become fan' for example, then with their choice initiate the  
function and
ask for the appropriate input for that function. This is the code  
below:


main :: IO()
do putStr Hi there! what is your name: 
fanName = getLine
do putStr 1 = Insert film, 2 = Become a Fan, 3 = The number of  
fans of a

film, 4 = Film released in a year: 
input = getLine
read input :: Int
(if input == 1 then main x = insertFilm [] else if input == 2 then  
main x =

becomeFan [] else if input == 3 then main x = numberOfFans [])

Say they choose film in a given year function function:

filmsInGivenYear :: Int - [Film] - [String]
filmsInGivenYear filmYear films = [ title | (Film title director  
year fans)

- films, year == filmYear]

I need to ask the user what filmYear they want to insert. But i  
need to do
this from the main function...I chose to do an if statement to  
choose what

function they want, but i dont know where to go from there?

any help will be greatly appreciated.

thanks

apple
--
View this message in context: http://www.nabble.com/IO-help- 
tp23423403p23423403.html
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




PGP.sig
Description: Signierter Teil der Nachricht
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[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 Henning Thielemann


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

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 Daniel Fischer
Am Donnerstag, 8. Mai 2008 14:59 schrieb 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?

Not a recommendable way. But there's no need to, your programme will probably 
look like

main = do
args - getArgs
let realArgs = parseArgs args
catalogue - loadData
doSomething realArgs catalogue

and doSomething might e.g. construct an updated catalogue and write it to a 
file.
___
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 Henning Thielemann


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?


Yes, but you do not want that. You will go on writing functions which 
consume Catalogue and finally you will apply a Catalogue function in the 
IO monad which does all the work at once.

___
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 Henning Thielemann


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

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 Thomas Davie


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


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