[Haskell-cafe] IO Monad

Bernard Pope bjpop at cs.mu.OZ.AU
Wed Jul 20 02:32:38 EDT 2005


On Wed, 2005-07-20 at 00:21 +0200, yin wrote:
> Dinh Tien Tuan Anh wrote:
> 
> >
> > Hi,
> > Could anyone explain for me why its not possible to return a primitive
> > type (such as Integer, String) while doing some IO actions ?
> >
> > e.g: foo :: IO() -> String
> >
> > What does it have to do with "lazy evalution" paradigm ?
> >
> In short, to not break functional aproach. Non-IO functions can't call
> IO functions, because IO functions are evaluated every time you call them.

I prefer to say it another way.

I think you asking for a function like this:

   f :: IO a -> a

If so, with this you could write:

   someChar :: Handle -> Char
   someChar handle
      = f (hGetChar handle)

where hGetChar :: Handle -> IO Char
and Handle represents the interface to a file.

This is a big problem for a purely functional language, because it means
someChar is not a function! Given the same Handle argument, successive
calls to someChar might return different results. Functions, by
definition, are not allowed to have this kind of behaviour.

Therefore f is not allowed. 

Cheers,
Bernie.



More information about the Haskell-Cafe mailing list