Hello!
Quote:
>I need to read a file and manipulate it's contents like a string. How does
>one convert IO() to String?
One cannot (frankly, one *can* in some Haskell implementations with
a function called unsafePerformIO :: IO a -> a, but as the name indicates,
it is unsafe).
Quote:
>If I could get this function to work I think I should be able to get to
>concept.
>> lineCount :: FilePath -> Int
>> lineCount fp = (length.lines.readFile) fp
>The error from this is that lines can't take IO, it needs a string.
Because reading a file is an interaction with the outside world,
counting the number of lines in the file is, too. So
lineCount should have this type signature:
lineCount :: FilePath -> IO Int
(lineCount maps a file path to an IO action yielding an Int when
it is eventually performed).
Because IO is a functor, you can lift a function of type
a -> b
to type
IO a -> IO b
with map.
So, you can define this:
lineCount :: FilePath -> IO Int
lineCount = map (length . lines) . readFile
Or you can use the do notation like this:
lineCount path = do
content <- readFile path
return $ (length . lines) content
The type signature is the same.
Regards,
Felix.