[Haskell-cafe] Functions with side-effects?

Creighton Hogg wchogg at login01.hep.wisc.edu
Wed Dec 21 10:35:30 EST 2005

On Wed, 21 Dec 2005, Daniel Carrera wrote:

> Creighton Hogg wrote:
> > x is a String, getLine has type IO String.  That's what I 
> > was getting at in one of my last e-mails.
> Hmm... let's see if I understand:
> * getLine() has type IO String.
> * The <- will "convert" an IO String to a plain String
> * So if I do  x <- getLine() then x has the type String.
> So, the <- effectively ammounts to an IO a -> a conversion.

Yeah, pretty much.  You're saying that "x is the result of 
action getLine", and in terms of types it means you're 
getting the a out of m a.
> In another email John Hughes said that one could think of "IO a" as a 
> set of instructins for obtaining a. I guess that means that IO is a sort 
> of imperative layer that helps the purely functional code interact with 
> the outside world.
> So I can have an IO bit (e.g. a do-block) that calls functions (which 
> are purely functional code) but I can't have a function that executes 
> any IO.
> For example, it is not possible to write a function "my_read_file" that 
> could work like this:
> my_data = my_read_file("my_file.txt")
> Correct? Otherwise this would be a function that is not referentially 
> transparent.

Assuming I understand correctly, then you are right.  "=" 
implies definition.  my_data is the symbol assigned to the 
result of a computation, not a defined constant or function.
That's at least how I think of referential transparency.

More information about the Haskell-Cafe mailing list