[Haskell-beginners] IO Problem

Brent Yorgey byorgey at seas.upenn.edu
Fri Oct 17 14:11:55 EDT 2008

On Fri, Oct 17, 2008 at 07:58:45PM +1300, Jamie McCloskey wrote:
> All this creates a whole load of functions with IO, even though it
> should not be necessary. What I would like to know, is how I can avoid
> this, possibly by modifying exec to just take a State.

The short answer is: it IS necessary.  Think about what exec does, for
instance.  It takes a Program and an initial State and does... what?
Executes the program, which might result in some things getting
printed to the screen or some input being read from the keyboard.
That is, exec can have some I/O effects.  Therefore, exec's return
type must be an IO value.  The same goes for all the other functions
you mentioned. 

This is a feature, not a bug --- the type of a function tells you
precisely whether it can possibly have any I/O effects.  If the return
type is (IO something), then it can; if the return type does not
involve IO, then it is *guaranteed*[1] that it cannot have any I/O


[1] Except for that pesky unsafePerformIO.  But you should never use
that, unless you really absolutely know what you are doing and why.

More information about the Beginners mailing list