Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
- data IO a :: * -> *
- class Monad m => MonadIO m where
- liftIO :: MonadIO m => forall a. IO a -> m a
- type FilePath = String
- readFile :: MonadIO m => FilePath -> m Text
- writeFile :: MonadIO m => FilePath -> Text -> m ()
- appendFile :: MonadIO m => FilePath -> Text -> m ()
- interact :: MonadIO m => (Text -> Text) -> m ()
- getContents :: MonadIO m => m Text
- getLine :: MonadIO m => m Text
- putStr :: MonadIO m => Text -> m ()
- putStrLn :: MonadIO m => Text -> m ()
The IO monad
A value of type
is a computation which, when performed,
does some I/O before returning a value of type IO
aa
.
There is really only one way to "perform" an I/O action: bind it to
Main.main
in your program. When your program is run, the I/O will
be performed. It isn't possible to perform I/O from an arbitrary
function, unless that function is itself in the IO
monad and called
at some point, directly or indirectly, from Main.main
.
IO
is a monad, so IO
actions can be combined using either the do-notation
or the >>
and >>=
operations from the Monad
class.
IO class
Files and handles
File and directory names are values of type String
, whose precise
meaning is operating system dependent. Files can be opened, yielding a
handle which can then be used to operate on the contents of that file.
File-at-a-time operations
readFile :: MonadIO m => FilePath -> m Text Source #
Read a file and return its contents as a string. The file is
read lazily, as with getContents
.
writeFile :: MonadIO m => FilePath -> Text -> m () Source #
Write a string to a file. The file is truncated to zero length before writing begins.
Special cases for standard input and output
interact :: MonadIO m => (Text -> Text) -> m () Source #
The interact
function takes a function of type Text -> Text
as its argument. The entire input from the standard input device is
passed (lazily) to this function as its argument, and the resulting
string is output on the standard output device.
getContents :: MonadIO m => m Text Source #
Lazily read all user input on stdin
as a single string.