| Safe Haskell | Safe |
|---|---|
| Language | Haskell2010 |
Test.Testable.IO.Base
Synopsis
- data IO a
- getLine :: IO String
- getChar :: IO Char
- getContents :: IO String
- interact :: (String -> String) -> IO ()
- readIO :: Read a => String -> IO a
- readLn :: Read a => IO a
- putChar :: Char -> IO ()
- putStr :: String -> IO ()
- putStrLn :: String -> IO ()
- print :: Show a => a -> IO ()
- readFile :: FilePath -> IO String
- writeFile :: FilePath -> String -> IO ()
- appendFile :: FilePath -> String -> IO ()
- runIOLines :: (Int -> String) -> IO a -> (a, [String])
- runIOLines' :: (Int -> Maybe String) -> IO a -> (a, [String])
- fmapIO :: (a -> b) -> IO a -> IO b
- pureIO :: a -> IO a
- appIO :: IO (a -> b) -> IO a -> IO b
- bindIO :: IO a -> (a -> IO b) -> IO b
IO replacements
IO is opaque, as in Prelude, but it does not interract with outer world
therefore it is safe and escapable by means of runIOLines
Internally, we use ST to accumulate user output and line generator
to represent input.
getContents :: IO String #
The getContents operation returns all user input as a single string, which
is read lazily as it is needed
interact :: (String -> String) -> IO () #
The interact function takes a function of type as
its argument. The entire input from the standard input device is passed to
this function as its argument, and the resulting string is output on the
standard output device.String -> String
print :: Show a => a -> IO () #
The print function outputs a value of any printable type to the
standard output device.
Printable types are those that are instances of class Show; print
converts values to strings for output using the show operation and
adds a newline.
For example, a program to print the first 20 integers and their powers of 2 could be written as:
main = print ([(n, 2^n) | n <- [0..19]])
appendFile :: FilePath -> String -> IO () #
The computation function appends the string appendFile file strstr,
to the file file.
running
runIOLines :: (Int -> String) -> IO a -> (a, [String]) #
Same as runIOLines', but does not allow finite input.
runIOLines' :: (Int -> Maybe String) -> IO a -> (a, [String]) #
Run virtual IO
This function accepts line genrator and (virtual) IO action to run
- generator: a function which for given line number returns line of input. Lines are numbered from 0, each line must not contain newline ('n') character, not even at the end.
- action: a virtual IO action
- return value is tuple (output of IO action, list of lines produced by
the
IOaction). If no lines are produced then[""]returned. Also, empty line at the end is to be expected if 'n' was last character on output (for example ifputStrLnwas last action).
>>>runIOLines show $ return 0(0, [""])>>>runIOLines show $ getLine >>= print . (+1) . read >> getLine >>= \x -> return (read x :: Int)(1, ["1",""])