3 Replies - 613 Views - Last Post: 05 February 2011 - 09:05 AM

#1 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 195
  • View blog
  • Posts: 1,620
  • Joined: 13-March 10

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?

This post has been edited by darek9576: 26 January 2011 - 05:00 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Haskell. Saving user input.

#2 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 590
  • View blog
  • Posts: 2,792
  • Joined: 05-January 09

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.
Was This Post Helpful? 2
  • +
  • -

#3 Shane Hudson  Icon User is offline

  • D.I.C Technophile
  • member icon

Reputation: 341
  • View blog
  • Posts: 1,281
  • Joined: 06-December 09

Re: Haskell. Saving user input.

Posted 05 February 2011 - 08:35 AM

Raynes, isn't darek9576 asking how to bind a value to a name?

ie.

Prelude> let a = "HELP!"
Prelude> a
"HELP!"
Prelude> "I need some " ++ a
"I need some HELP!"


Was This Post Helpful? 0
  • +
  • -

#4 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 590
  • View blog
  • Posts: 2,792
  • Joined: 05-January 09

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1