[Haskell-cafe] Functions with side-effects?

Creighton Hogg wchogg at login01.hep.wisc.edu
Wed Dec 21 07:15:17 EST 2005

On Wed, 21 Dec 2005, Daniel Carrera wrote:

> Hi all,
> I'm a Haskell newbie and I don't really understand how Haskell deals 
> with functions that really must have side-effects. Like a rand() 
> function or getLine().
> I know this has something to do with monads, but I don't really 
> understand monads yet. Is there someone who might explain this in newbie 
> terms? I don't need to understand the whole thing, I don't need a rand() 
> function right this minute. I just want to understand how Haskell 
> separates purely functional code from non-functional code (I understand 
> that a rand() function is inevitably not functional code, right?)

Well, it's *all* functional fundamentally but when people 
say "non-functional" in relation to Haskell code what they 
mean is in a monad.  Monads, I believe, can be just thought 
of as containers for state.  
So like I said in an e-mail on the tutorial thread the only 
distinction you need to make is between "=" which is truly a 
statement of definition and not subject to change, and "<-" 
which says that the left hand side is the result of the 
action of the right hand side.  In terms of types, "<-" 
strips away the monadic type. 

Note for example that if you want your main program to take 
in a string and then print it to the screen, it is *NOT*
main = putStrLn . getLine (which was I first thought)
main = putStrLn (getLine) (which you might guess from the 
type sig)
but rather

main = do
	x <- getLine
	putStrLn x

Because you want putStrLn to act on the string that 
*resulted* from getLine, and not on getLine itself.

I'd check out this link from HaWiki on monads,

More information about the Haskell-Cafe mailing list