String vs IO ()

Ketil Z. Malde ketil@ii.uib.no
23 Jan 2002 10:07:05 +0100


"S.D.Mechveliani" <mechvel@math.botik.ru> writes:

> I am sorry for, maybe, a silly question.

>   f name = readFile name >>= putStr

> has the type  String -> IO ()
> And I need    String -> String,

Then you're out of luck.  You can't get at the contents of the file
named "name" without doing IO.  You can't do IO outside of the IO
monad.  And once in, you never get out of it, it contaminates
everything up to the top level.

Purity sucks. :-)

Of course, you can do all that with unsafePerformIO, but you probably
shouldn't.

What you probably can do, is something like

main = do
        -- some actions or functions to get the value of 'name', either:
        name <- <actions>
        -- or:
        let name = <a pure function>
        contents <- readFile name
        -- actions or functions that use 'contents' as a normal string
        putStr (show $ tails contents) -- or whatever

-kzm
-- 
If I haven't seen further, it is by standing in the footprints of giants