Safe Haskell | None |
---|
Pure IO monad, intended for educational use.
- runIO :: Input -> IO a -> (Either Interrupt a, Output)
- data IO a
- data Input = Input {
- inputStdin :: ![String]
- inputFiles :: !(Map String String)
- data Output = Output {
- outputStdout :: ![String]
- outputFiles :: !(Map String String)
- data Interrupt
- data IOException
- putStrLn :: String -> IO ()
- putStr :: String -> IO ()
- getLine :: IO String
- readLn :: Read a => IO a
- print :: Show a => a -> IO ()
- readIO :: Read a => String -> IO a
- throw :: IOException -> IO a
- catch :: IO a -> (IOException -> IO a) -> IO a
- readFile :: FilePath -> IO String
- writeFile :: FilePath -> String -> IO ()
- appendFile :: FilePath -> String -> IO ()
- doesFileExist :: FilePath -> IO Bool
- removeFile :: FilePath -> IO ()
- getDirectoryContents :: FilePath -> IO [FilePath]
The IO monad and its machinery
runIO :: Input -> IO a -> (Either Interrupt a, Output)Source
Run the IO monad. This should be called in succession. Depending on the type of interrupt, this function should be re-run with the same action but with additional input.
User input.
Input | |
|
IO monad output.
Output | |
|
Something that interrupts the flow of the IO monad.
InterruptStdin | When you receive this interrupt, you should
get some standard input from somewhere and then
provide it in the |
InterruptException !IOException | When you receive this interrupt, you should consider the computation as ended. |
Library of actions
data IOException Source
An IO exception.
print :: Show a => a -> IO ()Source
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]])
throw :: IOException -> IO aSource
Throw an IO exception.
readFile :: FilePath -> IO StringSource
The readFile
function reads a file and
returns the contents of the file as a string.
The file is read lazily, on demand, as with getContents
.
writeFile :: FilePath -> String -> IO ()Source
The computation writeFile
file str
function writes the string str
,
to the file file
.
appendFile :: FilePath -> String -> IO ()Source
The computation appendFile
file str
function appends the string str
,
to the file file
.
Note that writeFile
and appendFile
write a literal string
to a file. To write a value of any printable type, as with print
,
use the show
function to convert the value to a string first.
main = appendFile "squares" (show [(x,x*x) | x <- [0,0.1..2]])
doesFileExist :: FilePath -> IO BoolSource
The operation doesFileExist
returns True
if the argument file
exists, and False
otherwise.
removeFile :: FilePath -> IO ()Source
removeFile
file removes the directory entry for an existing
file file.
getDirectoryContents :: FilePath -> IO [FilePath]Source
Get all files in the given directory.