Problems with IO

Ch. A. Herrmann herrmann@fmi.uni-passau.de
Tue, 24 Sep 2002 13:30:56 +0200


Hi Pavel,

>>>>> "pavel" == pavel  <pavel@joker.u.pereslavl.ru> writes:

    pavel> I'm having a problem with writing a function dealing with
    pavel> I/O. Maybe it's just a lack of experience or simple Haskell
    pavel> knowledge because I'm just a beginer. The problem: I want to
    pavel> write a function that converts an IO String into String lala
    pavel> :: IO String -> String

    pavel> Is it possible? If yes, how?

you cannot use (except from a dirty trick) an IO operation that
returns a string (IO String) as a string. What you can do 
--and, I assume, that's what you want to do-- is
to apply a function inside a composition of IO operations that uses the
result of a previous IO operation, e.g., function "f" in the
following program text:

main = do
        string1 <- readFile "input"  <<or something else of type IO String>>
        let result = f string1
        print result                 <<or something else>>
        return ()

Note that this kind of "let" does not have a corresponding "in". 

The reason that a function like unIO :: IO a -> a
does not legally exist is that it would break a nice
theoretical property of Haskell called referential transparency
which makes reasoning about programs extremely productive.

Hope that helps.
--
 Christoph Herrmann