Dusan Kolar <[EMAIL PROTECTED]> wrote,
> To access files I'm using standard file access and operators
> defined in class Monad. The problem is, that when I count
> the lines, I'm imprisoned by IO monad. Is there any way
> (e.g. by converting value to String and obtaining this string as a result)
> how to break IO monads? How to make a function of type
>
> f :: IO [Char] -> [Char]
You don't want this :-)
Let us assume you had such a function and implement
lineCount :: FilePath -> Int
and use it in
let lines = lineCount "/var/log/messages"
in
lines == lines
which should always be `True' of course. But now for some
weird reason, the Haskell compiler decides that it is better
to compute
lineCount "/var/log/messages" == lineCount "/var/log/messages"
instead (forget about the efficiency for a moment, according
to Haskell's semantics, the compiler is allowed to do this).
Now it will read the file two times, but maybe the contents
of the file changes inbetween the two calls and the result
of the expression is `False'. This means that the
optimisation of the compiler broke your program. If your
functions has type
lineCount :: FilePath -> IO Int
this problem can not happen.
Cheers,
Manuel