Haskell Language IO Writing to stdout


Example

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 () - writes a String to stdout

Prelude> putStr "This is a string!"
This is a string!Prelude>  -- Note, no new line

putStrLn :: String -> IO () - writes a String to stdout and adds a new line

Prelude> putStrLn "Hi there, this is another String!"
Hi there, this is another String!

print :: Show a => a -> IO () - writes a an instance of Show to stdout

Prelude> print "hi"
"hi"
Prelude> print 1
1
Prelude> print 'a'
'a'
Prelude> print (Just 'a')  -- Maybe is an instance of the `Show` type class
Just 'a'
Prelude> print Nothing
Nothing

Recall that you can instantiate Show for your own types using deriving:

-- In ex.hs
data Person = Person { name :: String } deriving Show
main = print (Person "Alex")  -- Person is an instance of `Show`, thanks to `deriving`

Loading & running in GHCi:

Prelude> :load ex.hs
[1 of 1] Compiling ex             ( ex.hs, interpreted )
Ok, modules loaded: ex.
*Main> main  -- from ex.hs
Person {name = "Alex"}
*Main>