Input / Output Actions
Haskell uses a special type IO
to distinguish impure, input/output actions from pure expressions. The idea of the IO
type is that apart from returning some value, it may also interact with the outside world along the way. The IO
type has the following structure:
where IO
is the type name, and a
is the parameterised value that it returns. For example:
The last example IO ()
represents an action that is run solely for its side effects and simply returns an empty tuple (a void value or unit). Some basic actions in Haskell are:
Note that, strictly speaking, putChar
and return
are not actions, but functions that return actions. The return
function is simply our one-way bridge from the pure world to the impure world that we use when we want to use pure values in actions, which we will see in examples soon. For now, let's just try out the basic actions getChar
and putChar
in GHCi:
Last updated