This post has been edited by darek9576: 26 January 2011 - 05:00 PM
Haskell. Saving user input.
Page 1 of 13 Replies - 613 Views - Last Post: 05 February 2011 - 09:05 AM
#1
Haskell. Saving user input.
Posted 26 January 2011 - 04:59 PM
Is it possible in Haskell to save user input to a variable that then the variable would be used in another function?
Replies To: Haskell. Saving user input.
#2
Re: Haskell. Saving user input.
Posted 26 January 2011 - 05:07 PM
In Haskell, there is really no such thing as a 'variable'. There are simply values that are bound to names. Reading user input is side-effect. It's I/O. Not pure. Thus, all I/O has to happen out of the main function. You can't do I/O anywhere you want. If you have a function that does I/O, it has to be called from within the main function or from a function that is called from the main function and so on.
In your main function, you can read some input and then pass that along to the functions that need it. You can't define a top-level value that is the result of I/O. Not without super hax anyway.
In your main function, you can read some input and then pass that along to the functions that need it. You can't define a top-level value that is the result of I/O. Not without super hax anyway.
#3
Re: Haskell. Saving user input.
Posted 05 February 2011 - 08:35 AM
#4
Re: Haskell. Saving user input.
Posted 05 February 2011 - 09:05 AM
He is asking how to bind the result of an IO action to a name in the top-level so that it can be used by other functions. The short answer is "you don't" and the long answer is to just bind the IO action to a name in your main function or a function called from your main function because the root of all IO is the main function. You still have to pass it around to functions that need it, as it wont be at the top-level and available everywhere. Finally, it also wont be an actual variable. You wont be able to rebind it. To simulate mutation of a value, you'd want to wrap it in an IORef or similar construct.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|