Dusan Kolar writes:
> Playing with IO monads I made to me a task. Create
> a function which counts lines of a file and returns the
> number of lines as a result:
> 
>   lineNo :: String -> Int
>             fname     number of lines
> 
> 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]
> 
> I know, that usually it is possible to write whole the
> program such a way so that it would not matter to have
> a IO monad as a top type, but having a function which counts number
> of lines in the file (or watever else) is also not too bad,
> at least as fas as I think, but maybe I'm completely wrong.
> 
>   Thanks a lot
> 
>                Dusan Kolar

Dusan,

in Haskell there is no such thing because Haskell is referentially
transparent language.  In Haskell extensions, like ghc, Hugs, there is
a function unsafePerformIO :: IO a -> a, which does exactly what you
desire.  But beware: it is unsafe, nonstandard, and generally regarded
as a Bad Thing in programming style.

Good luck.

-- Libor

Reply via email to