[Haskell-cafe] What are side effects in Haskell?

Adrian Neumann aneumann at inf.fu-berlin.de
Tue Dec 23 13:30:53 EST 2008


Am 23.12.2008 um 15:16 schrieb Hans van Thiel:

> Hello All,
>
> I just saw somewhere that one of the purposes of monads is to capture
> side effects. I understand what a side effect is in C, for example.  
> Say
> you want to switch the contents of two variables. Then you need a  
> third
> temporary variable to store an intermediate result. If this is global,
> then it will be changed by the operation.

But the two variables have also changed. After all they have  
different values after the switch. You see, even locally changing a  
variable is a side-effect. It changes the state of the program. Pure  
Haskell programs on the other hand have no notion of state, there are  
no variables which can change their value. Every time you want to  
manipulate something you're actually generating an new copy. You  
mustn't think of a haskell program as a series of changes to some state.

However when you *do* want state you can simulate it with a monad.  
The IO Monad is a special case here, since its actions don't change  
your program, they change the "world" the program is running in  
(writing files etc.). getLine etc are functions when you think of  
them as taking a hidden parameter, the state of the world. So getChar  
would become

getChar :: World -> (Char,World)

but the world stays hidden inside the IO Monad.

Regards,

Adrian

-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 194 bytes
Desc: Signierter Teil der Nachricht
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20081223/06035d31/PGP.bin


More information about the Haskell-Cafe mailing list