[Haskell-cafe] About print and side-effects

Anders Höckersten chucky at dtek.chalmers.se
Sun Dec 18 15:41:53 EST 2005

sön 2005-12-18 klockan 20:22 +0000 skrev Daniel Carrera:
> Hi all,
> The recent responses to my first question (thanks guys!) included the 
> following bit:
> main = print (fact 42)
> Now, print is a side-effect. Shouldn't it involve a do-block or a nomad 
> or one of those scary things you hear about when learning about side 
> effects in functional programs? How come 'print' is allowed to exist 
> alone if it's a side-effect?
> Thanks for the help.
> Cheers,
> Daniel.

Daniel, first of all I would like to invite you to #haskell on
irc.freenode.net. There are almost always people there to answer quick
questions like this one, with less of the waiting period that's involved
in a mailing list. You are of course also welcome to continue asking
your questions here if you prefer.

To answer your question: Yes, your main function does in fact involve
the IO monad. You can easily check the type of main or any function you
write by typing for example ":t main" in GHCi or Hugs. If you do this,
you will see that main has the type signature "main :: IO ()". The
reason you don't need do-blocks for this particular function is because
it only involves one computation. If you have more than one, you need
do-notation (well, there's a more correct and longer explanation for all
this, but we can save that one for later).

Oh, and don't mistake monads for scary things, they are really quite
warm and fuzzy once you get to know them. :)

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Detta =?ISO-8859-1?Q?=E4r?= en digitalt signerad
Url : http://www.haskell.org//pipermail/haskell-cafe/attachments/20051218/b4e16524/attachment.bin

More information about the Haskell-Cafe mailing list