Tutorial by Examples

main = do input <- getContents putStr input Input: This is an example sentence. And this one is, too! Output: This is an example sentence. And this one is, too! Note: This program will actually print parts of the output before all of the input has been fully read in. This m...
main = do line <- getLine putStrLn line Input: This is an example. Output: This is an example.
readFloat :: IO Float readFloat = fmap read getLine main :: IO () main = do putStr "Type the first number: " first <- readFloat putStr "Type the second number: " second <- readFloat putStrLn $ show first ++ " + " ++ show se...
Like in several other parts of the I/O library, functions that implicitly use a standard stream have a counterpart in System.IO that performs the same job, but with an extra parameter at the left, of type Handle, that represents the stream being handled. For instance, getLine :: IO String has a cou...
A bit counter-intuitive to the way most other languages' standard I/O libraries do it, Haskell's isEOF does not require you to perform a read operation before checking for an EOF condition; the runtime will do it for you. import System.IO( isEOF ) eofTest :: Int -> IO () eofTest line = do ...
In Haskell, it often makes sense not to bother with file handles at all, but simply read or write an entire file straight from disk to memory†, and do all the partitioning/processing of the text with the pure string data structure. This avoids mixing IO and program logic, which can greatly help avoi...
To make a Haskell program executable you must provide a file with a main function of type IO () main :: IO () main = putStrLn "Hello world!" When Haskell is compiled it examines the IO data here and turns it into a executable. When we run this program it will print Hello world!. If y...
Haskell is a pure language, meaning that expressions cannot have side effects. A side effect is anything that the expression or function does other than produce a value, for example, modify a global counter or print to standard output. In Haskell, side-effectful computations (specifically, those wh...
A common question is "I have a value of IO a, but I want to do something to that a value: how do I get access to it?" How can one operate on data that comes from the outside world (for example, incrementing a number typed by the user)? The point is that if you use a pure function on data ...
Per the Haskell 2010 Language Specification the following are standard IO functions available in Prelude, so no imports are required to use them. putChar :: Char -> IO () - writes a char to stdout Prelude> putChar 'a' aPrelude> -- Note, no new line putStr :: String -> IO () - writ...
As-per the Haskell 2010 Language Specification, the following are standard IO functions available in Prelude, so no imports are required to use them. getChar :: IO Char - read a Char from stdin -- MyChar.hs main = do myChar <- getChar print myChar -- In your shell runhaskell MyChar...

Page 1 of 1